When I add the following rule to my .htaccess, it works.
RewriteRule ^(.+)$ /index.php?p=$1 [L,QSA]
When I add the following rule, without the one above, it also works.
RewriteRule ^(.+)/(.+)$ /index.php?p=$1&p2=$2 [L,QSA]
However, when I use both rules together, with the second before the first, I get an Internal Server Error.
What's wrong here?
This is because the first rule also rewrites /index.php to itself.
You need to use a negitive lookahead based regex to exclude the /index.php
RewriteRule ^(.+)/(.+)$ /index.php?p=$1&p2=$2 [L,QSA]
RewriteRule ^((?!index\.php).+)$ /index.php?p=$1 [L,QSA]
Related
I'm new to .htaccess and I've been working on a site where I have used URL slugs. Everything is working perfectly fine with slugs that have hyphens in them, but I get 404 error when I have a one word slug.
https://www.example.com/blog/example-blog works fine but https://www.example.com/blog/example throws a 404 error.
Below is the .htaccess code I'm currently using:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
RewriteRule ^([a-zA-Z0-9-]+)\/?$ index.php?url=$1 [NC]
RewriteRule ^([a-zA-Z0-9-]+)\/season\/([0-9]+)\/?$ index.php?url=$1&season=$2 [NC]
</IfModule>
I've searched everywhere on Search Engine but got no luck. Any help is highly appreciated.
Summary:
I'm looking for ways for .htaccess to accept a slug without a hyphen as those with hyphens are working fine.
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
This rule will catch the request /example and unconditionally rewrites it to example.php. Whereas /example-blog (with a hyphen) is ignored by this rule (because the regex ^([a-z]+)\/?$ does not match).
If this rule is required then add an additional condition that checks for the existence of the .php file before rewriting (otherwise this rule should be removed altogether). For example:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-z]+)/?$ $1.php [NC,L]
Now, only requests that actually map to .php files are rewritten.
UPDATE:
I've added the L flag to the above rule, although it will still work without.
So, in summary, your complete set of rules should look like this:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-z]+)/?$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?url=$1 [L]
RewriteRule ^([a-zA-Z0-9-]+)/season/([0-9]+)/?$ index.php?url=$1&season=$2 [L]
There's no need to backslash-escape slashes in the regex, so I've removed the unnecessary backslashes. The NC flag is superfluous on the last two rules since you are already matching a-zA-Z in the RewriteRule pattern. And I've added the L flag, since you want processing to stop after the rewrite.
The <IfModule> container is also not required.
I have recently re-coded a website and I have the following line which works great for making a friendly looking URL.
RewriteRule ^mydir/([^/]+)/?$ /page.php?menu=mydir&vid=$1 [L,QSA]
However, I have found out that there are some old links out there which have an altogether different look to them which I also need to rewrite.
So I tried this...
RewriteRule ^mydir/detail/\?id=([^/]+)?$ /page.php?menu=mydir&vid=$1 [L,QSA]
RewriteRule ^mydir/([^/]+)/?$ /page.php?menu=mydir&vid=$1 [L,QSA]
The url containing /detail/ does not rewrite though. Any ideas why?
You cannot match query string using RewriteRule. Change your rules to this:
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^mydir/detail/?$ /page.php?menu=mydir&vid=%1 [L,QSA]
RewriteRule ^mydir/([^/]+)/?$ /page.php?menu=mydir&vid=$1 [L,QSA]
I'm trying to set the first rule as optional, so both of these URLs will work:
/username
/username/history
/history
The username will never clash.
RewriteRule ^([a-z]+)/((history|analysis|messages|photos)?)$ pages/dashboard.php?username=$1&page=$2 [L]
With the above I have /username/history working. Cannot figure howto get the others.
EDIT: The above snippet is the result of trying to merge these three lines into one.
RewriteRule ^(history|analysis|messages|photos)?$ pages/dashboard.php?page=$1 [L]
RewriteRule ^([a-z]+)/(history|analysis|messages|photos)?$ pages/dashboard.php?username=$1&page=$2 [L]
RewriteRule ^([a-z]+)$ pages/dashboard.php?username=$1 [L]
You can use this rule:
RewriteRule ^(history|analysis|messages|photos)/?$ pages/dashboard.php?page=$1 [L,NC,QSA]
RewriteRule ^([a-z]+)(?:/(history|analysis|messages|photos))?/?$ pages/dashboard.php?username=$1&page=$2 [L,NC,QSA]
So, I've been working on htaccess since like yesterday. I have two Rewrite statements in my htaccess file.
RewriteRule ^.*wp-login\.php\?loggedout=true.*$ /not_found [R,L]
The above statement works. While
RewriteRule ^.*wp-login\.php\?action=login.*% /not_found [R,L]
...doesn't!
To make the second case work, I used the following statements.
RewriteCond %{QUERY_STRING} action=logout
RewriteRule ^wp-login\.php$ /not_found/? [R,L]
So, not using "?action=logout" in the RewriteRule in the second case seems to solve the problem.
Yes, the problem is solved, but I'd like to understand why. This is so puzzling.
Any help is appreciated. Thank you.
Your earlier rules are incorrect because QUERY_STRING cannot be matched in RewriteRule. RewriteRule only match request uri without query string.
So correct rules are:
RewriteCond %{QUERY_STRING} (^|&)action=logout(&|$)
RewriteRule ^wp-login\.php$ /not_found/? [R,L]
OR this to include both query parameters:
RewriteCond %{QUERY_STRING} (^|&)action=(login|logout)(&|$)
RewriteRule ^wp-login\.php$ /not_found/? [R,L]
I'm trying to match these three routes:
system/session
teams
teams/529f3d87b3f7e2c73d100000
I have the following rules:
RewriteRule ([-A-Za-z0-9]+)/([-A-Za]+)$ /index.php?__module=$1&__action=$2 [L,QSA]
RewriteRule ^([-A-Za-z0-9]+)$ /index.php?__module=$1&__action=index [L,QSA]
RewriteRule ([-A-Za-z0-9]+)/([-A-Za-z0-9]+)$ /index.php?__module=$1&__action=index&id=$2 [L,QSA]
However, when I goto system/session its catching the rule set for teams/529f3d87b3f7e2c73d100000 and making session = $_GET['id'] and not $_GET['__action']
Is there an obvious solution to this?
That's because your regex is wrong for the first rule:
RewriteRule ([-A-Za-z0-9]+)/([-A-Za-z]+)$ /index.php?__module=$1&__action=$2 [L,QSA]
You're missing the -z part of the range, and only matching "a".
You should also go ahead and add ^ matches to them all:
RewriteRule ^([-A-Za-z0-9]+)/([-A-Za-z]+)$ /index.php?__module=$1&__action=$2 [L,QSA]