Condense mod rewrite - .htaccess

Is there any way to condense this into one line:
RewriteRule \.html+(.*)$ index.php?$1 [L]
RewriteRule \.htm+(.*)$ index.php?$1 [L]
I have tried:
RewriteRule \.(html|htm)+(.*)$ index.php?$1 [L]
But it didn't have the desired effect. Basically I want to check if either the .html and .htm extension is part of the url.
Thanks

Why do you have that plus sign there? It should be like this:
RewriteRule \.html(.*)$ index.php?$1 [L]
RewriteRule \.htm(.*)$ index.php?$1 [L]
and condensed:
RewriteRule \.html?(.*)$ index.php?$1 [L]
or:
RewriteRule \.(html|htm)(.*)$ index.php?$1 [L]

Related

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]

mod rewrite right markup

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.

.htaccess 'IF' in rewrite rule?

This is my rule:
RewriteRule ^([^/]+)(/.+)? /negocio$2.php?shopURL=$1 [L,QSA]
If $2 doesn't exist (URL doesn't have two slashes), I want to echo '/' instead of '.php'. So the condition becomes:
RewriteRule ^([^/]+)(/.+)? /negocio/?shopURL=$1 [L,QSA]
How do I do this or archive the same result?
Pretty sure this is what RewriteCond is for!
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/([^/]+)(/.+)$
RewriteRule ^([^/]+)(/.+)? /negocio$2.php?shopURL=$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule ^([^/]+)(/.+)? /negocio/?shopURL=$1 [L,QSA]
Replace your code with this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ negocio/?shopURL=$1 [L,QSA]
RewriteRule ^([^/]+)(/.+?)/?$ negocio$2.php?shopURL=$1 [L,QSA]

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