mod_rewrite - friendly URLs and removing page extension - mod-rewrite

I need a little help with some mod_rewrite rules I am trying to use. I need to do 2 things
Change a dynamic URL so that it’s friendly (e.g. /content.php?CategoryID=1 change to /categories/1/)
Remove the .php extension on my pages (e.g. /page2.php to /page2)
Ok so I can get number 1 to work by itself with the following in my .htaccess file:
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteOptions Inherit
RewriteBase /
RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1
I am then trying number 2 and I'm running into a few problems. I am using the following:
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]
The above rerwrites the URL so it looks friendly (e.g. /page2/) but it hits my 404 page and I can't view it but my other page where I rewrite the URL still works (e.g. /category/1/).
This is my full .htaccess file:
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteOptions Inherit
RewriteBase /
RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]
ErrorDocument 404 /misc/404page.html
Any help would be greatly appreciated as I am new to all this mod_rewrite and regex stuff.

I know it's been a while, and I'm not 100% sure what you were trying to do, but I would probably be looking at:
RewriteRule ^category/([0-9]+)$ category/$1/ [R,L]
RewriteRule ^category/([0-9]+)/$ content.php?CategoryID=$1 [L]
RewriteRule \.php? - [L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteRule ^([^/?])$ $1.php [L]
I'd need to look at your .htaccess more closely and/or have a better idea of the structure of your site to give better information.

Related

redirect all pages including sub pages to new domain

I tried this:
RedirectMatch 301 (.*) http://olddomain.com$1
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
But all subpages are not redirected.
Try this,
RewriteEngine On
# Take care of www.old.com.au
RewriteCond %{HTTP_HOST} ^www.old.com.au$ [NC]
RewriteRule ^(.*)$ http://www.new.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^attachment_id=([0-9]*)$ [NC]
RewriteRule ^$ http://www.new.com/? [R=301,NE,NC,L]
It's simple, I was just using this to do some special rewriting for my own, here is your code:
Put this inside your /www/.htaccess file:
RewriteEngine on
// Rules to redirect to another domain
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301,NC]
Check http://www.inmotionhosting.com/support/website/redirects/setting-up-a-301-permanent-redirect-via-htaccess, for 3 more ways to make a redirection.
Are you setting directive AllowOverride All in your Apache config?
Is the mod_rewrite module working?

Redirection loop URL rewriting

I'm redirecting all my .php extensions to .html, but I'm getting a loop problem.
For example, from:
-mywebsite.com/country/france-php/paris.php
to
-mywebsite.com/country/france-php/paris.html
Here is my .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule (.+)\.php$ $1.html [R=301,L]
RewriteRule (.+)\.html$ $1.php [L]
FallbackResource /index.php
When analyzing with redirection detective I get many redirections to:
-mywebsite.com/country/france-php/paris.html
Do the first redirection based on raw request received:
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php
RewriteRule ^ /%1.html [R=301,L]
RewriteRule ^(.+)\.html$ /$1.php [L]
To load index.php as index.html too, you can add the following rewrite at the top:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} ^(.*)/$
RewriteRule ^ %1/index.php [R=301,L]
You can try your rules this way.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.+)\.php
RewriteRule ^ %1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)\.html$ $1.php [L]
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php([^\?\ ]*)
RewriteRule ^ %1.html%2 [L,R=301]
RewriteCond %{DOUMENT_ROOT}/$1.php -f
RewriteRUle ^(.+)\.html(?:(/.*)|)$ $1.php$2 [L]
The problem is that the rewrite engine loops and your two rules keep overwriting each other. By adding some conditions, you can prevent that.

mod-rewrite weird behaviour on rewriterule

My .htaccess file looks like this:
Options +FollowSymlinks
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
RewriteRule ^product/([0-9]+)/*. /tags.php?id=$1 [NC,L]
RewriteRule ^page/([a-zA-Z0-9_-]+)$ /site.php?page=$1 [NC,L]
I wanted the last rule to be:
RewriteRule ^site/([a-zA-Z0-9_-]+)$ /site.php?page=$1 [NC,L]
If I change the last rule, it does not work at all. I get a 404 not found.
I have no idea why it doesn't work, is the word 'site' a registered word or...?

How do you remove index.php from a url using mod_write, while still allowing php to see the path with index.php in it?

For the past 3 days I have been playing around with Apache's mod_rewrite trying to get it to remove index.php from my url, while php still needs to see it in the path.
Essentially PHP needs to see this
http://example.com/index.php/Page/Param1/Param2
While the user needs to see this
http://example.com/Page/Param1/Param2
What I have right now is the following in an htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Which was taken from another page, and is close to what I need. However this seems to be cutting off everything after the http://example.com/ part. How can I get mod_rewrite to show the user one thing and have php see something else?
This is the modified code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]
Change R=302 to R=301 once you're satisfied that it is working fine for you.
This rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Needs to look like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ /%1 [R=301,L]
Also note that RewriteRule ^(.*)$ index.php?$1 [L,QSA] doesn't create a URI that looks like this /index.php/Page/Param1/Param2, it creates a query string that looks like this: /index.php?Page/Param1/Param2. Which isn't at all what you said PHP needs to see.

Need mod_rewrite condition and rule to replace ".com" with ".org" on 1 virtual host

I have a series of mod_rewrite directives (shown below as Code 1) that run every server request through a custom PHP app.
Code 1
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Before I get to the last step in the mod_rewrites, I need to change any request to mydomain.com to mydomain.org. Code 2 below shows what I am thinking but it does not work. The request gives me 500 Internal Server error.
Code 2
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule ^ http://mydomain.org%{REQUEST_URI}
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Can someone offer a suggestion? Thanks
I asume your site is reachable by two domains, but you want all request to your site redirected to one of them. Then the correct code is:
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
You should place that code directly after the redirect line.
The second line makes sure that there is no redirect if no Host is given (to prevent a redirect loop in that case).
The [R=permanent,L] in the third line makes it a permanent redirect and prevents any further rule processing. The other rules will be processed once the redirect has taken place.
The full file would then be:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]

Resources