Viewed   65 times

I'm coding a small CMS to get a better understanding of how they work and to learn some new things about PHP. I have however come across a problem.

I want to use mod_rewrite (though if someone has a better solution I'm up for trying it) to produce nice clean URLs, so site.com/index.php?page=2 can instead be site.com/tools

By my understanding I need to alter my .htaccess file each time I add a new page and this is where I strike a problem, my PHP keeps telling me that I can't update it because it hasn't the permissions. A quick bit of chmod reveals that even with 777 permissions it can't do it, am I missing something?

My source for mod_rewrite instructions is currently this page here incase it is important/useful.

 Answers

2

One approach is to rewrite everything to a handling script

RewriteEngine on
RewriteBase /

# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s 

# pass the rest of the request into index.php to handle     
RewriteRule ^(.*)$ /index.php/$1 [L]

so if you have a request to http://yourserver/foo/bar/

what you actually get is a request to http://yourserver/index.php/foo/bar - and you can leave index.php to decide what to do with /foo/bar (using $_SERVER['PATH_INFO'] -tom)

You only need to modify .htaccess the first time. All future requests for inexistent files can then be handled in PHP.

You might also find the docs for mod_rewrite useful - but keep it simple or prepare to lose a lot of sleep and hair tracking down obscure errors.

Monday, August 8, 2022
1

EDIT

I completely setup your files on my machine

//  /.htaccess

RewriteEngine on

RewriteRule ^(public)($|/) - [L,NC]

RewriteCond $1 !^(index.php|robots.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

.htaccess in the public folder:

//  /public/.htaccess

Options -Indexes
RewriteEngine off

This disables rewriting like you wanted.

/public/                 -> 403 Forbidden
/public/favicon.ico      -> 200 File found
/public/not-existing.ext -> 404 File not found

Do you have a index.php in you public folder? Maybe you could remove that one..

What kind of machine your testing on?

I tested it on Linux + Apache 2 + PHP5.3

I can give you more support in the afternoon (my time +2 GMT)

EDIT 2

When I remove this line from /.htaccess is still works

RewriteRule ^(public)($|/) - [L,NC]

Everything is handled by the .htaccess in the public folder.

Maybe it's a caching problem in your browser. Try a different browser/clean up history/install app to remove cache.. (depending on what browser you're using)

Thursday, October 13, 2022
 
shardul
 
4

My best bet would be to simply add get variables to the regex, like so:

RewriteRule ^account/blogs/([0-9]+)/([^s?]+)/?(?(.*))?$ /account/blog.php?blogId=$1&$4 [L,QSA]

This would rewrite

/account/blogs/1/ThisIsWhereTheTitleGoes?delete=1

to

/account/blog.php?blogId=1&delete=1

It would also support additional variables.

Friday, November 18, 2022
 
dave_d
 
5

I use the exact same stack for writing applications ( Angular Front End / Slim API ). My first application ran into the same exact issue you were having, that being that both frameworks needed the .htaccess redirect and were fighting for control.

My solution was rather simple. Angular being the front end controller i placed directly in my public facing folder ( in my case /public_html ). Slim was placed in a folder inside my public folder named /api.

This way Angular has it's own .htaccess file in the public folder, and my API was accessible via the /public_html/api path and was able to have it's own .htaccess file.

My file structure:

/public_html
   .htaccess ( ANGULAR )
   { Angular Files }
   /api
     .htaccess
     { Slim Files ( index.php ) }

App URL: https://myapplication.com API URL: https://myapplication.com/api

Your Angular .htaccess should contain the redirect:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ https://yourapp.com/index.html [QSA,L]

Your Slim .htaccess should contain something like:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Wednesday, November 16, 2022
 
1

It is probably not the best thing to do. You need to at least check out your PHP error log for things going wrong ;)

# PHP error handling for development servers
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_flag log_errors on
php_flag ignore_repeated_errors off
php_flag ignore_repeated_source off
php_flag report_memleaks on
php_flag track_errors on
php_value docref_root 0
php_value docref_ext 0
php_value error_log /home/path/public_html/domain/PHP_errors.log
php_value error_reporting -1
php_value log_errors_max_len 0
Monday, September 19, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :