I have a URL on my site that works without a / but when I add a / it says there's a redirect loop... I'm struggling to see the problem here....
Options -indexes
RewriteEngine On
#
#
#php_value output_handler ob_gzhandler
php_value output_handler none
php_flag register_globals off
php_flag safe_mode off
ErrorDocument 404 /404
#
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
#
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
RewriteRule ^battery/([^/]+)$ /browser/product?sku=BATTERY+$1&type=battery
RewriteRule ^vehicles/([^/]+)/([^/]+)/([^/]+)/product([0-9]+)$ /browser/index.php?make=$1&model=$2&id=$3&%{QUERY_STRING} [L,NC]
RewriteRule ^vehicles/([^/]+)/([^/]+)/([^/]+)/([0-9]+)$ /browser/product.php?make=$1&model=$2&year=$3&id=$4&%{QUERY_STRING} [L,NC]
RewriteRule ^vehicles/([^/]+)/([^/]+)/([^/]+)$ /store/product/list.php?make=$1&model=$2&year=$3&%{QUERY_STRING} [L,NC]
RewriteRule ^vehicles/([^/]+)/([^/]+)$ /vehicle/make/model/year/list.php?make=$1&model=$2&%{QUERY_STRING} [L,NC]
RewriteRule ^vehicles/([^/]+)$ /vehicle/make/model/list.php?make=$1&%{QUERY_STRING} [L,NC]
RewriteRule ^vehicles/$ /vehicle/make/list.php{QUERY_STRING} [L,NC]
RewriteRule ^keyfinder /browser/product?id=1001552 [L,NC]
I probably would have done some of the regular expression different for the RewriteRule, but it seems to be working accept for that trailing slash. Add /? to the end of each one of your RewriteRules for vehicles, battery & keyfinder. The /? is saying to look for zero or one trailing slash at the end of the line.
Here is an example:
RewriteRule ^vehicles/([^/]+)/([^/]+)/?$ /vehicle/make/model/year/list.php?make=$1&model=$2&%{QUERY_STRING} [L,NC]
RewriteRule ^vehicles/([^/]+/?)$ /vehicle/make/model/list.php?make=$1&%{QUERY_STRING} [L,NC]
RewriteRule ^vehicles/?$ /vehicle/make/list.php{QUERY_STRING} [L,NC]
The changes above will solve the problem of the redirecting loop caused by the trailing slash, but not the redirecting loop itself. You might have expected your 404 page to show up instead. It didn't work because "ErrorDocument" is wrong. There is no extension for the 404 page.
So again as an example, if your error page is called 404.php, the line needs to look like this...
ErrorDocument 404 /404.php
Hope this helps
Is it possible to have the log of the rewrite engine ?
Add the following lines in your configuration (bellow the RewriteEngine On is enough) :
RewriteLog logs/rewrite_https.log
RewriteLogLevel 15
I suppose your others logs are in logs/
Related
I want to make my website temporarily accessible only at example.com/unfinished-version.
All URLs like /style/main.css or /public/style/main.css would point to /soon.html, then all URLs like /unfinished-version/style/main.css would point to URLs like /public/style/main.css.
The rule I wrote doesn't work, because /public/style/main.css or /public/some-page still works.
RewriteRule ^(?!unfinished-version).*$ soon.html [L,NC]
URLs like example.com/gibberish show soon.html, so it does partly work.
I'll show you full .htaccess, although I find it unnecessary. (The rules shouldn't interfere with this, plus the L flag should avoid processing of any further rules in the first place.)
.htaccess in the root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(?!unfinished-version).*$ soon.html [L,NC]
RewriteRule ^unfinished-version(.*)$ public/$1 [L,NC]
</IfModule>
.htaccess in public/:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>
P.S.: Not even RewriteRule ^(.*)$ soon.html [L,NC] works.
I'm writing an htaccess for handeling my links for seo friendly links. But now suddenly it says an 500 Internal Server Error?? Is there a limit on lines for htaccess?
.htaccess:
ErrorDocument 404 /404/404.html
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^choose/[a-zA-Z0-9]+)/?$ choose.php?gebruikersnaam=$1 [L,QSA,NC]
blabla, some stuff you don't need to see anymore.
There is a few ways to debug why your .htaccess is not working, the fastest one is if you have access to the web server config file so you can turn on RewriteLog which will pinpoint where it fails for you:
RewriteLog "/path_to_save_it/rewrite_log"
RewriteLogLevel 9
Another way would be by placing your rules inside IfModule directive:
<IfModule mod_rewrite.c>
# your rule comes here
</IfModule>
For example:
Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
ErrorDocument 404 /404/404.html
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^show/([0-9]+)/?$ show.php?photo=$1 [L,QSA,NC]
RewriteRule ^report/([0-9]+)/?$ report.php?photo=$1 [L,QSA,NC]
RewriteRule ^index/?$ index.php [L,QSA,NC]
RewriteRule ^album/?$ album.php [L,QSA,NC]
RewriteRule ^choose/?$ choose.php [L,QSA,NC]
RewriteRule ^choose/[a-zA-Z0-9]+)/?$ choose.php?gebruikersnaam=$1 [L,QSA,NC]
RewriteRule ^rules/?$ rules.php [L,QSA,NC]
RewriteRule ^winners/?$ winners.php [L,QSA,NC]
RewriteRule ^contact/?$ contact.php [L,QSA,NC]
RewriteRule ^forget-password/?$ password.php [L,QSA,NC]
RewriteRule ^forget-password/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ password.php?gebruiker=$1&wachtwoord=$2 [L,QSA,NC]
RewriteRule ^upload-picture/?$ upload.php [L,QSA,NC]
RewriteRule ^user-cp/?$ user-photo.php [L,QSA,NC]
RewriteRule ^user-cp/delete/([0-9]+)/?$ user-photo.php?delete=$1 [L,QSA,NC]
RewriteRule ^user-cp/yes/([0-9]+)/?$ user-photo.php?yes=$1 [L,QSA,NC]
RewriteRule ^logout/?$ logout.php [L,QSA,NC]
RewriteRule ^mail-second/?$ mail_second.php [L,QSA,NC]
RewriteRule ^activate/?$ activate.php [L,QSA,NC]
RewriteRule ^activate/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ activate.php?code=$1&gebruikersnaam=$2 [L,QSA,NC]
</IfModule>
If all your rules have no issue but mod_rewrite is not enabled you will get no errors so you will know mod_rewrite is not enable if you try any rules.
If you still get error then it means mod_rewrite is enable and 1 or more rules are right, like missing something perhaps.
However from the above you will only know you have a problem, by using the elimination method you will find the exact line or lines that are causing the issue, which is by commenting all lines and then uncomment one by one, testing what that line is supposed to do before moving to the next line.
This way you will pinpoint where the problem is.
I have several urls on a Joomla site which have been indexed and I need to 301 redirect them into some new pages. The old URL is formed like this:
http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=20
I want it to go to:
http://www.mydomain.com/en/family-members/family-disease
I tried using:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^/en/wfmenuconfig/family/family-disease/177-category-english$ http://www.www.mydoamin.com/en/family-members/family-disease%1 [R=301,L]
I've tried several answers on here but nothing seems to be working.
htaccess 301 redirect dynamic url
and
301 Redirecting URLs based on GET variables in .htaccess
Any ideas what I should try next? (I've tried a normal redirect 301)
You've almost got it. You need to remove the leading slash from your rule's pattern because it's removed from the URI when applying rules from an htaccess file:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease%1? [R=301,L]
You also don't need the http://www.www.mydoamin.com bit (2 sets of www). At the end of your target, you have family-disease%1, which means if start=20 then the end of your URL will look like: family-disease20. Is that right?
The new URL doesn't have the query string in it, so it is just stripping of the last URL path part. If you want it hardcoded
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease? [R,L]
or a little bit more flexible
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/.+$ /en/family-members/family-disease? [R,L]
or if you just want to keep two levels after en/wfmenuconfig
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/(.+?/.+?)/ /en/$1? [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
If you just want to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease, then you must try these directives:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease [R=301,L]
But if that's not what you want, but to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease$var then you could check this one:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease%1 [R=301,L]
Now, give this one a little more try if it will work. If it's not, then find any suspicious why this code is not working:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /en/
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^wfmenuconfig/family/family-disease/177-category-english /family-members/family-disease [R]
And go to http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$AnyNumber if it's redirecting into http://www.mydomain.com/en/family-members/family-disease just make sure that your web server have mod_rewrite.
I just wanted to throw this out there, I was also having trouble getting the RewriteRule to work. I have a client that upgraded to a WordPress powered site from .asp pages. What I had to do to get this to work is insert the RewriteCond and RewriteRule in the htaccess file BEFORE the "# BEGIN WordPress" section. Now it works just as it should.
This is posted way late, but hopefully it helps someone else out there running into the same issue.
Doesn't Work:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
Does Work:
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Order of operations must be important =)
Can someone help me understand this code?
# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC]
RewriteRule .? /%{REQUEST_URI}? [R=301,L]
Basically I have a site www.example.com that is generating a link to www.example.com/index.cfm? I need it to redirect to www.example.com for SEO duplication purposes. I managed to remove the index.cfm but the ? still stays there (www.example.com/?). The trailing slash is also removed just fine if it's the last character. I found this rule online but I'm getting a "RewriteCond: bad flag delimiters" alert in apache and it doesn't do anything.
I also have some pages like www.example.com/index.cfm?term=test for searching so I just want to get rid of the trailing question mark and not when I do have a query attached to it.
The error is in the RewriteCond. I need help understanding the condition and why it doesnt work not just the answer to it.
Just in case here is the entire htaccess:
RewriteEngine On
Rewritebase /
# remove trailing index.cfm
RewriteRule ^index.cfm(\?)?$ / [R=301,L]
# SEF URLs
SetEnv SEF_REQUEST false
RewriteRule ^[a-z\d\-]+/[a-z]\d+/? /index.cfm/$0 [NC,PT,QSA,E=SEF_REQUEST:true]
RequestHeader add SEF-Request %{SEF_REQUEST}e
RewriteCond %{HTTP:SEF_REQUES} ^true$ [NC]
RewriteRule . - [L]
# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC]
RewriteRule .? ^%{REQUEST_URI}? [R=301,L]
NOTE: I did search online/stackoverflow before posting and did not find a solution to my problem.
EDIT: Also I noticed that my RewriteRule ^index.cfm(\?)?$ / [R=301,L] is removing the index.cfm even if it's not the last thing in the url resulting in a 404 when i try searching something (www.example.com/index.cfm?term=test) If someone could correct me and EXPLAIN that would be great. Thanks you.
EDIT2: www.example.com/index.cfm?term=test&a=dh&j=dhjsi should NOT be redirected.
www.example.com/a/b/d/f/h/w/d should not be redirected.
www.example.com/index.cfm? and www.example.com/index.cfm should be redirected to www.example.com.
RewriteCond %{THE_REQUEST} ? HTTP [NC]
RewriteRule .? ^%{REQUEST_URI}? [R=301,L]
Isn't going to work, because ? is a reserved character for regular expressions and you'd need to escape it along with the space. Try:
RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
RewriteRule ^/?(index\.cfm)? /? [R=301,L]
Additionally, you want this rule under your # remove trailing index.cfm rule, and not at the very bottom.
1) Case 1: removing question mark
http://example.com/page/subpage/?YOURSTRING=blabla
to redirect to
http://example.com/page/subpage/
then in the beggining of .htaccess, insert:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} YOURSTRING=(.*)
RewriteRule ^(.*)$ /$1? [R=301,L]
</IfModule>
# if wordpres isnot installed in root folder, then edit the fourth line to this
# RewriteRule ^(.*)$ /YOUR-WORDPRESS-DIRECTORY/$1? [R=301,L]
2) Case 2: redirection from question mark to another link
http://example.com/index.php?YOURSTRING=blabla&id=44
to redirect to
http://example.com/page/subpage/
Use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} YOURSTRING=blabla&id=44
RewriteRule ^(.*)$ http://example.com/page/subpage/? [R=301,L]
</IfModule>
I have the following rules in .htaccess. Unfortunately, it does not work due to the last rule (everything else works fine). Why?
Options -Indexes
RewriteEngine On
RewriteRule ^(cdn) - [L]
RewriteRule ^admin/(.*)$ backend_0.0.1/index.php/$1 [QSA,L]
RewriteRule ^css/(.*)$ frontend_0.0.1/css.php/$1 [NC,QSA,E=no-gzip:1,L]
RewriteRule ^js/(.*)$ frontend_0.0.1/js.php/$1 [NC,QSA,E=no-gzip:1,L]
RewriteRule ^(.*)$ frontend_0.0.1/index.php/$1 [QSA,L]
If I replace the last line by:
RewriteRule ^(.*)$ frontend_0.0.1/index.php?q=$1 [QSA,L]
Then it suddenly starts to work but previous rules are skipped and only this last rule is applied. But I need rules to stop rewriting once the first one mathches.
You need to exclude the destinations you are redirecting to:
RewriteCond $1 !^(backend_0\.0\.1|frontend_0\.0\.1)/
RewriteRule ^(.*)$ frontend_0.0.1/index.php/$1 [QSA,L]