htaccess rewrite rule without slash - .htaccess

I am trying and googling for hours now and can't find a solution for my problem. I want to rewrite:
domain.com/profile.php?user=abc > domain.com/abc
I use:
RewriteRule ^(.*)/$ /profile.php?user=$1 [L]
which works only with a slash at the end. If i try it without the slash
RewriteRule ^(.*)$ /profile.php?user=$1 [L]
I get an Internal Server Error!
Complete .htaccess
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(css|jpg|gif|png|jar|js|html|htm|php)$
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteRule ^share/([^/]*)/$ /share/share.php?id=$1 [L]
RewriteRule ^(.*)/$ /profile.php?user=$1 [L]
<IfModule mod_deflate.c>
<FilesMatch "\.(html|php|txt|xml|js|css|svg)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE\s7 !no-gzip !gzip-only-text/html
Someboy have an idea what Im doing wrong?
Thanks,
Kornel

With your second try, you get a 500 because you create an infinite loop:
^(.*)$ will match profile.php?user=xxx again and again... Which is not the case when you try with ^(.*)/$ because (no trailing slash) this won't match profile.php?user=xxx
Instead, you can have it this way
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(css|jpg|gif|png|jar|js|html|htm|php)$
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteRule ^share/([^/]*)/$ /share/share.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ /profile.php?user=$1 [L]

Related

Remove slash from htaccess

sorry for my bad english but i am using a translator ..
I should remove (and redirect) all url with slashes to url without slash
https://www.backupc.it/assistenza-da-remoto/
https://www.backupc.it/assistenza-da-remoto
https://www.backupc.it/aa/
https://www.backupc.it/aa
the problem is that leaving the slashes redirects me to the 404 page but without css also for the existing pages (the existing ones if there is the slash it gives me 404 error)
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^www.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
ErrorDocument 404 /404.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
</IfModule>
I solved by trial and error (randomly)
I added the second part you see but I did not understand the meaning .. The fact is that it fell into place
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^www.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
DirectorySlash Off
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ $1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ErrorDocument 404 /404.html
</IfModule>

What does the below mentioned code do in .htaccess?

Could anyone please tell me what the below code does?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example-old.com$
RewriteRule (.*) http://www.example-new.com/$1 [R=301,L]
Does it work only if someone types exactly example-old.com? What would happen to the URLs with folders, like example-old.com/folder1 and example-old.com/folder1/sub1/page1.php
One of my client's website has become a disaster. Following is the code in .htaccess and pages on Google search when opened, it adds an extra '/' at the end of every URL which displays a horrendous page. I am unable to figure out where the error in code is:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example-old.com$
RewriteRule (.*) http://www.example-new.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example-old.com$
RewriteRule (.*) http://www.example-new.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example-new.com$
RewriteRule (.*) http://www.example-new.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
# remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
RewriteCond %{HTTP_HOST} ^example-old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example-old\.com$
RewriteRule ^2015\/?(.*)$ "http\:\/\/www\.example-old\.com\/$1" [R=301,L]
Instead of opening a page with the URL:-
http://www.example-new.com/folder1/page1.php
It opens:
http://www.example-new.com/folder1/page1.php/
I'm using example-old and example-new due to security reasons. Please help...!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example-old.com$
RewriteRule (.*) http://www.example-new.com/$1 [R=301,L]
This permanently redirects example.com/foo/foobar to example-new.com/foo/foobar
RewriteCond %{HTTP_HOST} ^example-old.com$
RewriteRule (.*) http://www.example-new.com/$1 [R=301,L]
in php context, you would use
if(host == "example.com" and Request_uri== "any request")
{header('location : host/Request_uri',301)
else
{no redirection}

How to disable SSL if a page url contains a specific word

I have my site running on all over SSL, but I added a games sections which uses the 3rd party games in flash. When I enable SSL it stop working. I am wondersing if how can I only disable the SSL on that specific page which contains "/game/" in the url so that the games will work fine.
I tried this code from insternet in my .htaccess file but not working:
RewriteCond %{HTTPS} off
RewriteRule ^game^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
EDIT:
I tried the answer suggested:
RewriteEngine On
RewriteCond %{HTTPS} On [NC]
RewriteRule ^game http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
It works but when I go the pages contained the word "game" it turns the other pages also to "http" and other pages are become using "http" instead of "https"
Then I tried this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} On [NC]
RewriteRule ^game http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
This it starts giving me the message "Page is not redirecting properly" when I go to "game" page url.
I want only the "game" pages to use "http".
EDIT TWO: The FULL .htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Get rid of index.php
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?rewrite=2 [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) index.php?rewrite=1 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} public\/ [OR]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
</IfModule>
# sends requests /index.php/path/to/module/ to "index.php"
# AcceptPathInfo On
# #todo This may not be effective in some cases
FileETag Size
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</IfModule>
Need help. Let me know if any duplicate is found, I can not find anyone though.
Thanks.
This should be your full .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# except for /game/ enforce https
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/game/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# for /game/ enforce http
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /game/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Get rid of index.php
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?rewrite=2 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_FILENAME} public [OR]
RewriteCond %{REQUEST_URI} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
</IfModule>
Make sure to clear your browser cache before testing this change.

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>

htaccess force www and keep trailing url

I am using the following to redirect my non www. urls to use www.:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "http://www.domain.com/"
</IfModule>
However, if someone visits domain.com/abc-123 it redirects to www.domain.com and loses the remaining of the url.
Any help with this? How can i set it up so it doesn't remove the end of the url?
Miro
Just swap the order of the rules around. Since the first (WP) rule is stripping the path, by the time the second rule is applied the path will be empty.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "http://www.domain.com/"
</IfModule>

Resources