I have recently moved to a new server for my website, and i activated mod_rewrite with success, but some files still not working !
Like this one:
RewriteRule ^activate\/(.*)\/(.*)$ activate.php?Email=$1&hash=$2
I have made all necessary changes on my VPS (Ubuntu) !
Any idea ?
Like this lines work well:
RewriteRule ^search\/(.*)\/([a-zA-Z0-9_-]+)$ search.php?view=$1&p=$2
RewriteRule ^search$ searchResults.php
RewriteRule ^term$ term.php
RewriteRule ^faq$ faq.php
# Non-working line...
RewriteRule ^activate\/(.*)\/(.*)$ activate.php?Email=$1&hash=$2
Note that mod_rewrite are enabled and all other lines work well only this one !
First thing, you must define a RewriteBase because of your two rules (first and last) that create both virtual directories.
Then, you should disable MultiViews option to make term or faq rule work without problem.
Finally, you can rewrite your patterns in a more generic way (or at least in a better way, more precise). Also, don't forget L flag ([L]) after each rule, otherwise mod_rewrite continues to evaluate next rules.
You can replace your current code in your htaccess by this one (assuming your htaccess is in root folder)
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^search/([^/]+)/([^/]+)$ search.php?view=$1&p=$2 [L]
RewriteRule ^search$ searchResults.php [L]
RewriteRule ^term$ term.php [L]
RewriteRule ^faq$ faq.php [L]
RewriteRule ^activate/([^/]+)/([^/]+)$ activate.php?Email=$1&hash=$2 [L]
Related
Since we migrated to a new server, some of our pages are broken (404). Reason is we have 2 broken rewrite rules.
What's really strange is that they work if I change folder's name.
For example this work:
RewriteRule ^anything/([a-zA-Z0-9-]+)/$ page.php?var=$1 [L]
This doesn't:
RewriteRule ^myfolder/([a-zA-Z0-9-]+)/$ page.php?var=$1 [L]
I can't even find a trick to make 301 redirects, because my original "myfolder/" virtual folder never matches.
Any ideas what's going on? I was thinking it could be a rule override or something like that (as it's hosted on a multidom solution), but i don't have such rules in my main site at the root. It drives me crazy.
Thx!
In practice you probably want to do 2 things. Disable multiviews and also bypass rules if the request is a real directory.
Options -MultiViews #turn off automatic URI matching, can cause weirdness
RewriteEngine on
#stop here if the request is a real file or directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^myfolder/([a-zA-Z0-9-]+)/?$ /page.php?var=$1 [L]
I'm trying to convert the following NGINX rule:
location ~ "^/calendrier/[0-9]{4}" {
rewrite ^/calendrier/(.*)$ /calendar/$1;
}
to .htaccess. I tried:
RewriteCond ^/calendrier/[0-9]{4} [NC]
RewriteRule ^/calendrier/(.*)$ /calendar/$1 [QSA,L]
but it isn't working.
Please help.
Thanks
You need to review the mod_rewrite documentation before attempting to convert rules from other platforms. Reason being: it is essential that you understand exactly how mod_rewrite should be used so that conversions are painless.
The problem with your conversion is that the RewriteCond is not checking that pattern against anything. Essentially, you've just done guess-work to see if it does what you want.
You only need to place the following in your /.htaccess file:
RewriteEngine on
RewriteRule ^calendier/([0-9]{4})/?$ /calendar/$1 [L]
The first part of the rule checks for calendier/<some_number> with an optional trailing slash (If you do not want the slash, you can remove /?).
This is the rewrite I used to get the job done.
RewriteCond %{REQUEST_URI} ^/calendrier/ [NC]
RewriteRule ^calendrier/(.*)$ /calendar/$1 [NC,L]
I have rewrite rules in a htaccess file working perfectly except for one weird issue that is not redirecting to the index.php like it should.
apache2 running on Debian
site.com/abc/xyz processes through site.com/index.php like it should
site.com/somethingelse/folderx/1234 processes through site.com/index.php like it should
site.com/accounts/ processes through site.com/index.php like it should
BUT
site.com/accounts/orders/1234 processes straight to site.com/accounts/orders.php without going to root/index.php like it is supposed to.
rewrite rules in htaccess:
RewriteRule \. - [L]
RewriteRule (.*)$ index.php [L]
Is there a simple reason that I am not seeing?
It looks as if you have Multiviews turned on. It's part of content negotiation and can cause apache to do unexpected stuff like what you're describing with /orders/ -> /orders.php. Try turning it off:
Options -Multiviews
RewriteRule \. - [L]
RewriteRule (.*)$ index.php [L]
The beginning of .htaccess is
RewriteEngine On
#begin of rules for administration folder, redirect to https, if not https
RewriteCond %{REQUEST_URI} ^/administration
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
#end of rules for administration folder
#===no rules for /administration folder below this line===
#the rest part of .htaccess
Why does the rest part of .htaccess still performs for https://www.mydomain.com/administration/index.php ? How to stop the performing of the rest part of .htaccess file for urls that follow to administration folder? What's wrong in my code?
Thank you.
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Add this line on top of your .htaccess file:
RewriteRule ^administration - [NC,L]
I had simmilar issue these days and I got a really interesting answer from the user DaveRandom in the PHP chat:
"Basically, mod_rewrite effectively runs in a loop (this isn't really true but effectively that's what it does) and the [L] flag is like a "continue" statement in PHP (it stops processing the rules below in the current iteration but it will then start processing the rules again from the top). So in your rules, the first iteration was matching the first rule, and then the second iteration skipped the first rule (it produced the same output as the input) and then matched the second rule."
Possible Solution: "if you are using apache 2.4 using [END] instead of [L] would be a fix".
It is because you have syntax error. .htaccess will ignore erroring lines
Change this:-
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
To this:-
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
I've tried every single example I could find, they all produce an internal server error. I have these rules set up (this works, no error):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^((/?[^/]+)+)/?$ ?q=$1 [L]
So if it's not an existing file or an existing directory with an index.php we redirect. For instance, http://domain.com/foo/bar becomes http://domain.com/?q=foo/bar
Thing is, I want the trailing slash stripped. So take off the /? at the end of the rule. How do I make it so that http://domain.com/foo/bar/ becomes http://domain.com/foo/bar with a visible redirect first (fixing the client's URL), and only then the real, silent redirection to ?q=?
Everywhere I look I see this:
RewriteRule (.*)/$ $1 [R,L]
But it gives me a 500 error if I insert it before my rule.
If foo/bar exists as a real directory, then the server will be redirecting the client to foo/bar/ (with the trailing slash). It has to do that in order for relative URLs to work correctly on the client. If you put in a rule to rewrite that back to foo/bar with a redirect then there will be a loop. An easy way to test if that's happening is to specify a path that doesn't exist at all (I assume from your index.php detection that the directory tree actually exists). The nonexistent path won't trigger the built-in redirect.
If I setup a similar set of rules to yours (plus the suggested slash-removal rule) I can see the difference between a directory that exists and one that doesn't. The ones that don't work as expected, the ones that do cause Firefox to say This page isn't redirecting properly. IE8 says something similar. Perhaps the Apache setup you're using can detect it and turns it into the 500 error?
It looks like the simpler rewrite rule you mention at the end of your question should work. The problem is, the 500 error isn't really helpful in figuring out why it's not working. One way I've found useful in helping debug mod_rewrite errors is to enable it's logging. Add the following to your httpd.conf:
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
RewriteLogLevel 3
Then try again, and look in the log to see what's going on. Once you're done, you can disable the log be setting the rewriteloglevel 0. See the mod_rewrite docs for details.
Try this rule in front of your current rule:
RewriteRule (.*)/$ /$1 [R,L]
Try these rules:
#prevent mod_dir from adding slash
DirectorySlash Off
#redirect /folder/ to /folder
RewriteCond %{THE_REQUEST} ^GET\s\S+/(\?\S+)?\s [NC]
RewriteRule ^(.*)/$ /$1 [R=301,L,QSA]
#internal redirect for directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L]