user889030
user889030

Reputation: 4754

.htaccess url rewriting rule for multiple php files

i have about 100 php files and i want to rewrite their urls. They way i know to rewrite url for php file is something like

www.domain.com/article1/2
RewriteRule    ^article1/([0-9]+)/?$    article1.php?page=$1    [NC,L]    # Handle page requests

so like that for other files i have to write separate htaccess rules which will make file big and will take a lot of time to rewrite separate rule for every file. so i was think that is there anyway to rewrite these url for all files with single rule.

my urls are like

www.domain.com/article1.php?page=1
www.domain.com/photos.php?page=1
www.domain.com/tips.php?page=1

i would like to rewrite them to

www.domain.com/article1/1
www.domain.com/photos/1
www.domain.com/tips/1

i know with my above rule its possible but for that i have to use each rule for every file.

Upvotes: 2

Views: 1011

Answers (1)

arkascha
arkascha

Reputation: 42915

This is probably close to what you are looking for:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^/?([^/]+)/([0-9]+)/?$ $1.php?page=$2 [END]

That setup will work in the http servers host configuration and in dynamic configuration files (.htaccess style files), provided their interpretation is enabled at all (AllowOverride directive) and that the file is located at the correct location and readable for the http server.

If you are operating a really old version of the apache http server you may have to replace the END flag with the L flag...


A general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Upvotes: 1

Related Questions