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]
Related
I have hundreds of these old links I need to redirect.
Here is one example:
/index.php?option=com_content&view=article&id=433:seventh-character-code-categories-and-icd-10-cm&Itemid=101&showall=1
to
/seventh-character-code-categories-and-icd-10-cm
Essentially I need to remove the /index.php?option=com_content&view=article&id=433: part.
I tried this but I am getting confused with the [0-9] and : parts, so the following does not work:
RewriteRule ^/index.php?option=com_content&view=article&id=[0-9]:(.*)$ /$1 [L,R=301]
Say you want to capture from after : to right before & in the query string you mentioned, then try this expression:
^[^\:]*\:([^\&]*)\&.*$
As #starkeen mentioned in comments, you got to check against the query string. This can be done using RewriteCond %{QUERY_STRING}
So if index.php is in the root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ http://example.com/%1 [R=301,L]
Here's another example. This one is for a sub folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/pages\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ /pages/%1? [R=301,L]
Also, notice the ? at the end of the url /pages/%1?, this prevents from re-attaching the query string.
Another thing, captured groups will be set to variables %{number} since set in the RewriteCond.
BTW, depending on your server's configuration, you may need to add the NE flag, like [NE,L,R=301] Plus test whether it is necessary to double escape the literal characters.
what is about direct approach. Skip all till semicolon, mach string till & and replace all with first much
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} [^:]+:([\w-]+[^&]).*
RewriteRule .*$ \/%1? [R=301,L]
</IfModule>
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]
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]
Why does not work this regex? The general problem appears after simbol "?", Probable several options, but could not solve the problem.
RewriteEngine on
RewriteRule ^portfolio.php?id=(.*?)$ /index.php?page_id=$1
You cannot match query string in RewriteRule pattern, that is used for matching REQUEST_URI only. You will need a RewriteCond for that. Use this rule:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([^&]*) [NC]
RewriteRule ^portfolio\.php$ /index.php?page_id=%1 [L,QSA]
I am trying to accomplish 3 fairly standard url mods in my htaccess file, but unfortunately I am getting thrown in a loop. What am I doing wrong?
# force a trailing slash
RewriteRule ^profile/([^/]+)$ /profile/$1/ [R=301,L]
# check for trailing parameters
RewriteCond %{QUERY_STRING} ^(.*)$
# put it all together
RewriteRule ^profile/([^/]+)/$ /profile/index.php?username=$1&%1 [NC,L]
The pattern of your first rule, ^profile/([^/]+)$, does also match the destination of your second rule, profile/index.php.
You could simply exclude that file with an additional RewriteCond for the first rule:
RewriteCond $1 !=index.php
RewriteRule ^profile/([^/]+)$ /profile/$1/ [R=301,L]