mod rewrite right markup - .htaccess

I have mod rewrite expressions in .htaccess
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?lang=$1&cat=$2&ac=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?start=$1&lang=$2&cat=$3&ac=$4 [L]
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?lang=$1&cat=$2&bc=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?start=$1&lang=$2&cat=$3&bc=$4 [L]
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?lang=$1&cat=$2&cc=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?start=$1&lang=$2&cat=$3&cc=$4 [L]
but GET's of links are wrong because of same (repeating) conditions (for ac, bc, cc).
I have this kind of solution, but don't like this:
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([x]+[0-9a-z]+)$ /index.php?lang=$1&cat=$2&ac=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([x]+[0-9a-z]+)$ /index.php?start=$1&lang=$2&cat=$3&ac=$4 [L]
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([y]+[0-9a-z]+)$ /index.php?lang=$1&cat=$2&bc=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([y]+[0-9a-z]+)$ /index.php?start=$1&lang=$2&cat=$3&bc=$4 [L]
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([z]+[0-9a-z]+)$ /index.php?lang=$1&cat=$2&cc=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([z]+[0-9a-z]+)$ /index.php?start=$1&lang=$2&cat=$3&cc=$4 [L]
What is right solution for this.

Related

httaccess, all rewrite is redirecting to index.php

Hi i have a some problem with .htaccess, I need make url structure like this: https://example.com/folder/name1
folder - is directory with all files, .htaccess, indexes etc..
name1 - is parameter for backend, mysql etc...
In my .htaccess, I have this code:
RewriteRule ^(.*)/shop shop.php
RewriteRule ^(.*)/voucher/ voucher.php
RewriteRule ^(.*)/voucher voucher.php
RewriteRule ^(.*)/rules/ rules.php
RewriteRule ^(.*)/rules rules.php
RewriteRule ^(.*)/item/(.*)/ item.php?id=$2
RewriteRule ^(.*)/item/(.*) item.php?id=$2
RewriteRule ^(.*)/item/ item.php
RewriteRule ^(.*)/item item.php
RewriteRule ^(.*)/ index.php
RewriteRule ^(.*) index.php
And problem is with two last lines, because when I go to https://example.com/folder/name1 is working, I seed index.php file, but when I go https://example.com/folder/name1/shop I still see index.php. Where is problem?
All of your rewrite rules should have the "last" flag on them ([L]) which tells mod_rewrite not to process other rules after. What is happening is this your last rule matches all requests, even ones that have been previously rewritten.
RewriteRule ^(.*)/shop shop.php [L]
RewriteRule ^(.*)/voucher/ voucher.php [L]
RewriteRule ^(.*)/voucher voucher.php [L]
RewriteRule ^(.*)/rules/ rules.php [L]
RewriteRule ^(.*)/rules rules.php [L]
RewriteRule ^(.*)/item/(.*)/ item.php?id=$2 [L]
RewriteRule ^(.*)/item/(.*) item.php?id=$2 [L]
RewriteRule ^(.*)/item/ item.php [L]
RewriteRule ^(.*)/item item.php [L]
RewriteRule ^(.*)/ index.php [L]
RewriteRule ^(.*) index.php [L]

Htaccess add and force trailing slash

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

Apache log: Request exceeded the limit of 10 internal redirects

I have a web page that working ok in a server but in my new server is not working. The Apache Log says "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.".
I can't fix it.
<IfModule rewrite_module>
RewriteEngine on
RewriteRule ^(.*)\.css$ $1.css [L]
RewriteRule ^(.*)\.png$ $1.png [L]
RewriteRule ^(.*)\.jpg$ $1.jpg [L]
RewriteRule ^(.*)\.gif$ $1.gif [L]
RewriteRule ^(.*)\.js$ $1.js [L]
RewriteRule ^(.*)\.swf$ $1.swf [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ index.php?idioma=$1&codigo=$2&subcodigo=$4&subsubcodigo=$6 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ index.php?idioma=$1&codigo=$2&subcodigo=$4&subsubcodigo=$6 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)$ index.php?idioma=$1&codigo=$2&subcodigo=$4 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ index.php?idioma=$1&codigo=$2&subcodigo=$4 [L]
RewriteRule ^(.*)/(.*)/(.*)$ index.php?idioma=$1&codigo=$2 [L]
RewriteRule ^(.*)/(.*)$ index.php?idioma=$1&codigo=$2 [L]
RewriteRule ^(.*)/$ index.php?idioma=$1 [L]
RewriteRule ^/$ index.php [L]
</IfModule>
Those rules create an infinite loop
RewriteRule ^(.*)\.css$ $1.css [L]
RewriteRule ^(.*)\.png$ $1.png [L]
RewriteRule ^(.*)\.jpg$ $1.jpg [L]
RewriteRule ^(.*)\.gif$ $1.gif [L]
RewriteRule ^(.*)\.js$ $1.js [L]
RewriteRule ^(.*)\.swf$ $1.swf [L]
and are, by the way, useless (so you can remove them).
You have also many design issues in your code (you can combine several rules, you can use more restrictive regex, you can avoid capturing non-needed data, etc).
Finally, your htaccess would look like this
<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/[^/]+/([^/]+)/[^/]+/([^/]+)/?[^/]*$ /index.php?idioma=$1&codigo=$2&subcodigo=$3&subsubcodigo=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/[^/]+/([^/]+)/?[^/]*$ /index.php?idioma=$1&codigo=$2&subcodigo=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?[^/]*$ /index.php?idioma=$1&codigo=$2 [L]
RewriteRule ^([^/]+)$ /index.php?idioma=$1 [L]
RewriteRule ^$ /index.php [L]
</IfModule>

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]

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