Simple Htaccess question - .htaccess

RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{THE_REQUEST} \?abc=xyz(&(app=([a-z]+))) #i don't know it is right or wrong
########LOGIN########
RewriteCond %{THE_REQUEST} \?event_id=156&rp=([a-zA-Z0-9=]+)
RewriteRule ^events/login.php$ http://www.xyz.com/is2011/login.php?rp=%1 [R=301,L]
########SEARCH########SEARCH########################
RewriteCond %{THE_REQUEST} \?search=([a-z]+)&event_id=156&submit=Search
RewriteRule ^is2011/([a-z]+).php$ http://www.xyz.com/is2011/$1.php?search=%1 [R=301,L]
I just want to skip above rewrite rules if and only if there is app=xyz in query string.

Would be:
RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{THE_REQUEST} \?abc=xyz(&(app=([a-z]+))) #i don't know it is right or wrong
RewriteRule ^(.*)$ - [skip=2]
########LOGIN########
RewriteCond %{THE_REQUEST} \?event_id=156&rp=([a-zA-Z0-9=]+)
RewriteRule ^events/login.php$ http://www.xyz.com/is2011/login.php?rp=%1 [R=301,L]
########SEARCH########SEARCH########################
RewriteCond %{THE_REQUEST} \?search=([a-z]+)&event_id=156&submit=Search
RewriteRule ^is2011/([a-z]+).php$ http://www.xyz.com/is2011/$1.php?search=%1 [R=301,L]
The rewrite rule i added does not make any substitution in the query string matched by its RewriteCond. The skip flag skips the next num rules, in this case two. See RewriteRule's directive documentation at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule.

Related

.htaccess RewriteRule(s) not working

I am trying to learn how to use RewriteRule.
I have 2 'pages' (wordpress)-
domain.com/webinars/ (shows list of all webinars)
and
domain.com/webinar/ (shows specific webinar details)
I have trying to set the following conditions -
(1) domain.com/webinars/{Year}/{Month}/ will load /webinar/?year={Year}&month={Month}
(2) domain.com/webinar/ (with no /{Year}/{Month}) will load /webinars/
(3) domain.com/webinar/?year={Year}&month={Month} will redirect to /webinars/{Year}/{Month}/ and then apply condition #1
This is my attempted code
RewriteEngine On
RewriteRule ^/webinars/([0-9]+)/([A-Za-z0-9]+)/$ /webinar/?year=$1&month=$2 [NC]
RewriteRule ^/webinar/$ /webinars/
RewriteRule ^/webinar/year=(\d+)$month=([\w-]+)$ /webinars/%1/%2? [R=301,L]
Condition #1 results in a 404 page not found
Condition #2 shows /webinar/ and not /webinars/
Condition #3 stays on domain.com/webinar/?year={Year}&month={Month} and does not redirect
What am I doing wrong? Only other code in my htaccess file is the default wordpress block.
Try the following :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^webinars/([^/]+)/([^/]+) /webinar/?year=$1&month=$2 [NC]
The rest could be done the same way .
Try with below rules,
RewriteCond %{REQUEST_URI} ^/webinar$
RewriteCond %{QUERY_STRING} ^year=([^/]+)&month=([^/]+)
RewriteRule ^ http://example.com/webinars/%1/%2/? [R=302]
RewriteCond %{REQUEST_URI} ^/webinars/([^/]+)/([^/]+)/$
RewriteRule ^ webinar/?year=%1&month=%2 [L,END]
RewriteCond %{REQUEST_URI} ^/webinar/$
RewriteRule ^ webinars/ [L]
You can use this
RewriteEngine on
# if /webinar/ is requested skip the rules and serve /webinar/ directory
RewriteRule ^webinar/?$ - [L]
#redirect /webinar/?year={year}&month={month}
#To /webinar/year/month
RewriteCond %{THE_REQUEST} /webinar/\?year=([^\s&]+)&month=([^\s&]+) [NC]
RewriteRule ^.+$ /webinar/%1/%2? [L,R]
# rewrite new url to the old one
RewriteRule ^webinar/([^/]+)/([^/]+)/?$ /webinar/?year=$1&month=$2 [L,NC]

Htaccess rewriterule and special characters '&'

I am trying to make some really simple redirection from an old to a new website. But all my redirection with the character '&' dont work.
For exemple:
RewriteRule ^-Nos-equipes-$ /-Associes-Collaborateurs-?lang=fr [R=301,L]
work fine, but:
RewriteRule ^-Contact-?&lang=en$ /-Nous-contacter-?lang=en [R=301,L]
Dont work.
I am sure its easy, but i dont find anyway to make it work. I have try flag [NE] , or write [&]...
Thanks. Casp.
EDIT:
I now use the solution anubhava who works;
but not totaly cause my problem is more complexe;
In fact i got some url with param:
?&lang=en, ?&lang=fr, and ?lang=en
So i have had other query string like that:
# Redirection from the old site #--------------------------------------
RewriteCond %{QUERY_STRING} (^|&)lang=fr(&|$)
RewriteCond %{QUERY_STRING} (^|&)lang=en(&|$)
RewriteCond %{QUERY_STRING} ^lang=en(&|$)
This work still great; except in 2 cases:
the first one:
trying to make this redirection (stay on same url but juste modify the param)
RewriteRule ^the_url?&lang=en$ /the_url?lang=en [R=301]
who make an redirection error.
And second case when i have to rewrite the same url but with different param:
RewriteRule ^the_url$ /other_url?lang=fr [R=301]
RewriteRule ^the_url?&lang=en$ /other_url?lang=en [R=301]
RewriteRule ^the_url?&lang=fr$ /other_url?lang=fr [R=301,L]
It works but only with the first one insruction.
(i dont put the flag L, cause i got a long list of rewrite; i think i can put the condition at the beginning and not rewrite them each-time)
:::::::::::::::::::::::
Exemple of real url that i am trying to redirect who cause trouble
www.mywebsite/-Avocats-a-la-Cour- to--> www.mywebsite/?lang=fr
www.mywebsite/-Avocats-a-la-Cour-?&lang=en to--> www.mywebsite/?lang=en
www.mywebsite/Marque-francaise?&lang=fr to--> www.mywebsite//Marque-francaise?lang=fr
www.mywebsite/French-Trademarks?&lang=en to--> www.mywebsite//French-Trademarks?lang=en
www.mywebsite/-Contact-?&lang=en to--> www.mywebsite/-Nous-contacter-?lang=en
www.mywebsite/-Contact-?&lang=fr to--> www.mywebsite/-Nous-contacter-?lang=fr
www.mywebsite/person-780?lang=en to--> www.mywebsite/personel?lang=en
You cannot match QUERY_STRING in RewriteRule. You can use:
RewriteCond %{QUERY_STRING} (^|&)lang=en(&|$)
RewriteRule ^-?Contact-?$ /-Nous-contacter- [R=302,NC,L]
QUERY_STRING is automatically carried over to target URI.
EDIT: Based on your edited question you can have these rules:
# www.mywebsite/-Avocats-a-la-Cour- to--> www.mywebsite/?lang=fr
RewriteCond %{THE_REQUEST} \s/+(-Avocats-a-la-Cour-)\s [NC]
RewriteRule ^ /?lang=fr [R=302,L,NE]
# www.mywebsite/-Avocats-a-la-Cour-?&lang=en to--> www.mywebsite/?lang=en
RewriteCond %{THE_REQUEST} \s/+(-Avocats-a-la-Cour-)\?&?(lang=en)\s [NC]
RewriteRule ^ /?%2 [R=302,L,NE]
# www.mywebsite/Marque-francaise?&lang=fr to--> www.mywebsite/Marque-francaise?lang=fr
# www.mywebsite/French-Trademarks?&lang=en to--> www.mywebsite/French-Trademarks?lang=en
RewriteCond %{THE_REQUEST} \s/+(Marque-francaise|French-Trademarks)\?&(lang=(?:en|fr))\s [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]
# www.mywebsite/-Contact-?&lang=en to--> www.mywebsite/-Nous-contacter-?lang=en
# www.mywebsite/-Contact-?&lang=fr to--> www.mywebsite/-Nous-contacter-?lang=fr
RewriteCond %{THE_REQUEST} \s/+(-Contact-)\?&?(lang=(?:en|fr))\s [NC]
RewriteRule ^ /-Nous-contacter-?%2 [R=302,L,NE]
# www.mywebsite/person-780?lang=en to--> www.mywebsite/personel?lang=en
RewriteCond %{THE_REQUEST} \s/+(person-780)\?&?(lang=(?:en|fr))\s [NC]
RewriteRule ^ /personel?%2 [R=302,L,NE]
This doesnt look right
RewriteRule ^-Contact-?&lang=en$ /-Nous-contacter-?lang=en [R=301,L]
I would think the & should only be used when you have more than one parameter being passed
e.g.
?lang=en&userId=13
I would try:
RewriteRule ^-Contact-?lang=en$ /-Nous-contacter-?lang=en [R=301,L]

Redirect url with question mark and other characters

How would i redirect this url to www.example.com using a htaccess file.
used-machinery/en/other/used-machinery-item?000172:compost-turner
Would this work?
RewriteRule ^used-machinery/en/other/used-machinery-item?000172:compost-turner?$ http://www.example.com/ [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L]
Would the ? cause an issue in the old url?
The question mark should represent a query string, thus you would have to make write a condition to detect the query string. This should work
RewriteEngine On
ReWriteCond %{REQUEST_URI} ^/used-machinery/en/other/used-machinery-item$
ReWriteCond %{QUERY_STRING} ^000172:compost-turner$
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L]
Edit:
With a huge bunch or URLs you can use the "OR" flag or perhaps even try something on the server side. It would be a pain but I don't know any other solution with a huge amount of links.
RewriteEngine On
# %{THE_REQUEST} format as follows
# GET /used-machinery/en/other/used-machinery-item?000172:compost-turner HTTP/1.1
# important notes:
# escape ? with( \?)
# space after url (\ )
# each line has [NC,OR] flags for case insensitive and checks this condition or the next
# last Cond line does not have the OR flag
ReWriteCond %{THE_REQUEST} ^.+\ /used-machinery/en/other/used-machinery-item\?000172:compost-turner\ .*$ [NC,OR]
ReWriteCond %{THE_REQUEST} ^.+\ /anoterURL\?querystring\ .*$ [NC,OR]
ReWriteCond %{THE_REQUEST} ^.+\ /lastURL\?with-no-OR-at-end\ .*$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L]
If you wish to a a mass edit, I suggest using Sublime Text
Paste all links with one link per line
Turn word wrap off
Select a ? in a URL
In the menu, Find > Quick Find All (to select all ?s on the page)
Press left cursor arrow
type the \
Press the End key to go the end of all lines
type \ .*$ [NC,OR]
Press the Home key to go the beginning of all lines
type ReWriteCond %{THE_REQUEST} ^.+\
Edit the last row to remove the ,OR
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} 000172:compost-turner [NC]
RewriteRule ^used-machinery/en/other/used-machinery-item/?$ /? [R=301,L]
RewriteRule ^((?!index\.php/).*)$ index.php/$1 [L,NC]

Remove query string - rewrite rule

How do i remove a query string from the end of a url?
To be more specific, this is my rewrite rule:
RewriteRule example-(.*).html$ examples/view-example.php?param1=parameter&param2=$1&split=-
and I want this to return a 404 or a redirect to www.mydomain.com/example-one.html :
www.mydomain.com/example-one.html?param1=parameter&param2=one&split=-
This is what i tried, it doesn't work:
RewriteCond %{REQUEST_URI} /examples/view-example\.php
RewriteCond %{QUERY_STRING} param1=parameter&param2=(.*)&split=-
RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$
I think that RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$ isn't correct..
This should do the trick:
RewriteCond %{QUERY_STRING} ^param1=parameter&param2=(.*)&split=-
RewriteRule ^/examples/view-example\.php$ http://mydomain.com/example-%1.html [R=301]
Though, I didn't understand what you wanted to do with www.mydomain.com/example-one.html?param1=parameter&param2=one&split=-

HtAccess rewriting, removing a part of the text between the first two slashes and adding index.php after it

I got the following url:
127.0.0.1/abc_123456/default/index/index/
Which should be rewritten to:
127.0.0.1/123456/index.php/default/index/index/
So remove abc_ and add index.php after it. Problem is that the digits are variable, but the abc_ isn't.
I had the following rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_
RewriteRule ^abc_(.*)/(.*)$ /$1/index.php/$2
But that resulted in the url being rewritten to:
127.0.0.1/123456/default/index/index.php/index/
Seems like I'm almost there, but I can't figure it out.
Thanks in advance
Use this simple rule:
RewriteRule ^abc_([0-9]+)/(.*)$ $1/index.php/$2 [L,NC]
Try
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_([0-9]+)/([^\ \?]+) [NC]
RewriteRule ^ /%1/index.php/%2 [L]
EDIT
However, you are right about the other rule, that's the one giving the error; RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ RewriteRule ^(.*)$ /index.php/$1 That one is used to rewrite if the page does not contain the /abc_123456/
Add an extra condition to that rule as below
#if not abc_
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ [NC]
#if not already index.php
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
for example, if you need similar solution, when visited this url:
http://yoursite.com/subpage1/subpage2/?YOURSTRING=blabla
to redirected visitor to
http://yoursite.com/subpage1/subpage2/
then see link - http://stackoverflow.com/a/15680832/2215124

Resources