Htaccess add and force trailing slash - .htaccess

I am having some trouble with my htaccess code:
RewriteEngine on
RewriteRule ^about\/$ /about.php [L]
RewriteRule ^about?$ /about/ [L,R]
RewriteRule ^contact\/$ /contact.php [L]
RewriteRule ^contact?$ /contact/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery\/$ /loc-gallery.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery?$ /$1/gallery/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/location\/$ /loc-location.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/location?$ /$1/location/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/contact\/$ /loc-contact.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/contact?$ /$1/contact/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/$ /loc-home.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)?$ /$1/ [L,R]
The problem is that if i want to access:
http://example.com/about
http://example.com/contact
i am not redirected to
`http://example.com/about/` and `http://example.com/contact/`
and when i try to access http://example.com/ i am redirected to http://example.com//
What i am doing wrong? Thanks in advance!

When you try to access http://example.com/ i am redirected to http://example.com// is happening because of this line :
RewriteRule ^([A-Za-z0-9\-]+)?$ /$1/ [L,R]
It will match the empty URI because of this regex ^([A-Za-z0-9\-]+)?$
it means match if ([A-Za-z0-9\-]+) then or not ? .
It should look like this :
RewriteRule ^([A-Za-z0-9\-]+)$ /$1/ [L,R]
I also sumerized your code and fixed some issues like this :
RewriteEngine on
RewriteRule ^about(\/)?$ /about.php [L]
RewriteRule ^contact(\/)?$ /contact.php [L]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery(\/)?$ /loc-gallery.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/location(\/)?$ /loc-location.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/contact(\/)?$ /loc-contact.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/$ /loc-home.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)$ /$1/ [L,R]
Clear browser cache and test it , if it is ok change R to R=301 to get permanent redirection

Related

URL Re Writing issue double http issue

I am having a issue with my urls google have crawled double http for example
http://example.com/example.com/c-title-id.html
and all the interlinked pages like that
please can any one help me out to redirect all the urls with 301 with .htacess
My htacess code is
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]
RewriteRule category-([0-9]+)-(.*)-([0-9]+)\.html$ category.php?cid=$1&name=$2&page=$3.php [L]
RewriteRule search-(.*)-([0-9]+)\.html$ search.php?term=$1&page=$2.php [L]
RewriteRule all-covers-([0-9]+)\.html$ all-covers.php?page=$1.php [L]
RewriteRule index-([0-9]+)\.html$ index.php?page=$1.php [L]
RewriteRule featured-covers-([0-9]+)\.html$ featured-covers.php?page=$1.php [L]
RewriteRule top-downloaded-([0-9]+)\.html$ top-downloaded.php?page=$1.php [L]
RewriteRule c-([0-9]+)(.*)\.html$ cover.php?id=$1&name=$2.php [L]
RewriteRule sitemap.xml$ sitemap.php [L]
RewriteRule sitemap1.xml$ sitemap1.php [L]
RewriteRule rss.xml$ rss.php [L]
RewriteRule ^(.*)\.html$ $1.php [L]
ErrorDocument 404 /404.html
I want to redirect all of them automaticaly.
Should be as simple as having this at the top of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]
RewriteRule example\.com/(.*)$ /$1 [R=301,L]
#... rest of your rules...

Ensure rewrite-rule is used

I have created a simple rewrite URL like this:
RewriteRule ^messages$ /messages.php [L]
How do I force the user to use /messages and not /messages.php? I.e., if user queries messages.php, he'll be 301 redirected to /messages.
I have a long list of rewrite rules, like the one above.
RewriteEngine On
RewriteRule ^messages$ /messages.php [L]
RewriteRule ^events$ /events.php [L]
RewriteRule ^cv$ /cv.php [L]
RewriteRule ^profile/([^/]*)$ /profile.php?id=$1 [L]
RewriteRule ^ratings$ /ratings.php [L]
RewriteRule ^newsfeed$ /newsfeed.php [L]
RewriteRule ^logout$ /logout.php [L]
RewriteRule ^profile$ /profile.php [L]
RewriteRule ^ranking$ /rank.php [L]
RewriteRule ^find-study$ /search.php [L]
RewriteRule ^search/$ /search.php [L]
RewriteRule ^search$ /search.php [L]
RewriteRule ^invite-friends$ /invite-friends.php [L]
RewriteRule ^profile/([^/]*)$ /profile.php?id=$1 [L]
RewriteRule ^profile/([^/]*)/([^/]*)$ /profile.php?id=$1&programme=$2 [L]
RewriteRule ^student-requests$ /student-requests.php [L]
# Q&A
# view question
RewriteRule ^question(?:/([^/]+)(?:/([^/]*))?)?/?$ /question.php?id=$1&permalink=$2 [L]
RewriteRule ^question/([^/]*)/([^/]*)/([^/]*)$ /question.php?id=$1&permalink=$2&answer=$3 [L]
# manage question
RewriteRule ^ask(?:/([^/]+)(?:/([^/]*))?)?/?$ /manageQuestion.php?tag_id=$1&tag_type=$2 [L]
RewriteRule ^ask/([^/]*)/([^/]*)/([^/]*)$ /manageQuestion.php?tag_id=$1&tag_type=$2&tag_name=$3 [L]
RewriteRule ^editquestion(?:/([^/]+)(?:/([^/]*))?)?/?$ /manageQuestion.php?id=$1&second=$2 [L]
# questions
RewriteRule ^questions$ /questions.php [L]
RewriteRule ^questions/([^/]*)$ /questions.php?first=$1 [L]
RewriteRule ^questions/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2 [L]
RewriteRule ^questions/([^/]*)/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2&third=$3 [L]
RewriteRule ^questions/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2&third=$3&fourth=$4 [L]
When trying to solve this problem you probably encountered the problem of infinite loops. One way to prevent this is to use %{THE_REQUEST}, since this variable doesn't change on a rewrite. Therefore, if you use the following rule, it will change every url ending on .php to an url without it:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(messages|events|cv|ratings|etc)\.php\ HTTP
RewriteRule ^ %2 [R,L]
After testing that the rule works correctly, change [R,L] to [R=301,L] to make the redirect permanent. Doing this before everything works correctly makes testing a pain.
Edit: Instead of using (.*) you could use /(page1|page2|page3) for all the simple redirects. For each of the more complex ones you probably need to have a single redirect each in the form of:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /questions\.php\?id=([^&]+)&permalink=([^&]+)&answer=([^&]+)\ HTTP
RewriteRule ^ questions/%2/%3/%4 [R,L]

mod_rewrite config for /

I have the following .htaccess config
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule \.git - [F,L]
RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
RewriteRule ^users/([0-9]+) profile.php?id=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?u=$1 [L]
Now, whenever somebody visits the landing page, they get redirected using the last rule for profile.php?u=$1.
How do I change the configuration so that www.example.com and www.example.com/ are mapped to index.php and not profile.php?
Match the empty string or single slash just after the ^index rule:
RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
# Match root request with optional slash
RewriteRule ^/?$ index.php [L]
I will suggest not to do it this way.
Instead, simply user this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This will send all your requests to index.php page, from there create a router.php and pass on the requests to that page, using php.
but in case you do, just add
RewriteRule ^/?$ index.php [L]
Like Michael suggested.
Here is a simple tool to test your rules, if you wish to
Apache RewriteRule tester

What's wrong with this rewrite rule?

I have this .htaccess:
RewriteEngine On
RewriteRule ^!/(.*)$ path/to/a/file/$1 [L]
RewriteRule ^(.*)$ path/to/another/file/$1 [L]
I want urls in the form of www.website.com/!/this/ to be rewritten to path/to/a/file. Any URL that doesn't match that pattern should be rewritten to path/to/another/file/.
Here's what I've tried to far:
RewriteEngine On
RewriteRule ^!/(.*)$ path/to/a/file/$1 [L]
RewriteCond ...
RewriteRule ^(.*)$ path/to/another/file/$1 [L]
When using the above rewrite rule, I get a 500 - Internal Error.
How can I fix it?
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/!
RewriteRule ^!/([a-z0-9_\-\.]+) user/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/!
RewriteRule ^([a-z0-9_\-\.]+)/([a-z0-9_\-\.]+)/?$ $1/controller/front.php/$2 [L]
RewriteCond %{REQUEST_URI} !^/!
RewriteRule ^([a-z0-9_\-]+)/([a-z0-9_\-]+)/([a-z0-9_\-]+)/?$ $1/controller/front.php/$2/$3 [L]

what is wrong with this htaccess multi level directory rewrite rule?

I have some problems with my htaccess...
I want to have my url's like this:
http://example.com/artist/
http://example.com/artist/rihanna/
http://example.com/artist/rihanna/biography/
http://example.com/artist/rihanna/video/
http://example.com/artist/rihanna/news/
The problem is all of the url's work except for "http://example.com/artist/"
RewriteRule ^artist/([^_]*)/biography/$ /artist-biography.php?name=$1 [L]
RewriteRule ^artist/([^_]*)/biography?$ /artist/$1/biography/ [R=301,L]
RewriteRule ^artist/([^_]*)/video/$ /artist-video.php?name=$1 [L]
RewriteRule ^artist/([^_]*)/video?$ /artist/$1/video/ [R=301,L]
RewriteRule ^artist/([^_]*)/news/$ /artist-news.php?name=$1 [L]
RewriteRule ^artist/([^_]*)/news?$ /artist/$1/news/ [R=301,L]
RewriteRule ^artist/([^_]*)/$ /artist.php?name=$1 [L]
RewriteRule ^artist/([^_/]+)$ /artist/$1/ [R=301,L]
RewriteRule ^artist/$ /artist-page.php [L]
RewriteRule ^artist?$ /artist/ [R=301,L]
This line
RewriteRule ^artist/([^_]*)?$ /artist/$1/ [R=301,L]
will match http://example.com/artist/ which is probably not what you wanted. Change it as below
RewriteRule ^artist/([^_/]+)$ /artist/$1/ [R=301,L]
If that does not fix it completely, let me know the result.

Resources