I'm going to apply the following rewrite rules on my dispatcher
RewriteRule ^content/feeds/networkapp/ios/1.0/config(.*)?$ /content/feeds/ios/feed.config.json [L,NC]
RewriteRule ^content/feeds/networkapp/ios/((\d\.)+\d/)?config(.*)?$ /content/feeds/ios/$1feed.config.json [L,NC]
Does first rewtite rule will be executed?
I need to revrite :
...../ios/1.0/config to /ios/feed.config.json
...../ios/x.x.x/config to /ios/x.x.x/feed.config.json
As you may see,I have used [L] flag. This means that if the rule matches, no further rules will be processed.
RewriteRule ^content/feeds/networkapp/ios/1.0/config(.*)?$ /content/feeds/betnow/ios/feed.config.json [L,NC]
RewriteRule ^content/feeds/networkapp/ios/((\d\.)+\d/)?config(.*)?$ /content/feeds/ios/$1feed.config.json [L,NC]
[L] flag on apache documentation
Related
I have a URL :
https://www.example.com/login/login.cgi?user=option
Want to exactly match login.cgi
If user hits https://www.example.com/login/login.cgi need to redirect to external site say www.redirect.com
If anyone hits https://www.example.com/login/login.cgi?user=option it should not cause any redirection.
I tried various option of query string and as well Rewrite rules seems to not work.
I tried using :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^login.cgi$
RewriteRule ^login/$ https://www.redirect.com? [R=302,L]
This should work for you.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?login/login\.cgi$ https://example.com [R,L]
This will redirect yoursite.com/login/login.cgi to example.com .
The condition RewriteCond %{QUERY_STRING} ^$ matches empty querystring meaning that when the the URI is /login/login.cgi without any querystring the Rewriterule will be applied.
The /? in the beginning of RewriteRule pattern makes it possible to use the rule in both contexts server.config or in htaccess.
I'm trying to avoid multiple redirects. My current set up includes the following:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^\/?(.*)$ https://www.example.com/$1[R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
What I need is a rule that forces HTTPS, WWW and adds the trailing slash to all urls with one rule.
The above is forcing the urls to run through two redirects.
I've searched everywhere and was able to find one rule for https / www and remove trailing slash in one rule, but not one for adding the slash.
Thank you
From https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/ the answer appears to be:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
You can have multiple conditions before each rule.
Alternatively just change the [L] flag in your existing RewriteRules to [N] to process all your rules in one go.
rewriting the url RewriteRule ^about/ /about.php [L,R=301] but the page is accessed via /about.php directly also. Please help to how to redirect .php file
Replace your code with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+about\.php\s [NC]
RewriteRule ^ /about/ [R=302,L]
RewriteRule ^about/?$ /about.php [L,NC]
You can use this free .htaccess Redirect Generator:
http://htaccessredirect.net/
with this tool you can easily create your redirection code for .htaccess.
You can use rule to hidden redirect:
#use [L] to hidden [R] to shown redirect
RewriteRule ^(about|faq|home)/?$ $1.php [L]
#to prevent access from about.php simply do this:
RewriteRule (.+)\.php$ $1/ [R=301]
#you need to know 301 redirect will be cached with browser for long time
#so if you change rule - will be no effect for some visitors
This is my mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
# ————————————————————————-
# > URL REWRITING
# ————————————————————————-
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)/diario$ hotsite/diary.php
RewriteRule ^(.*)/recados$ hotsite/messages.php
RewriteRule ^(.*)/fotos$ hotsite/photos.php
RewriteRule ^(.*)/videos$ hotsite/videos.php
RewriteRule ^(.*)/contato$ hotsite/contact.php
RewriteRule ^([a-z0-9._\-]+)$ hotsite/index.php [L]
It works just like facebook profiles. Whenever I type "mywebsite.com/user.name" it goes to that user page. I can also type "mywebsite.com/user.name/videos" to go to a specific page in the user profile.
However, I can't access "mywebsite.com" anymore, because it redirects to "mywebsite.com/hotsite/index.php". How can I disable this behavior and leave the redirects only when someone types a user name at the end?
Thank you very much.
The RewriteCondition(s) only affect the RewriteRule that comes immediately after that. So, to stop redirecting htp://mywebsite.com/aDirThatExists like http://mywebsite.com/aboutUs you need to repeat the rewrite conditions as
RewriteCond %{SCRIPT_FILENAME} !-d # if not a directory
RewriteCond %{SCRIPT_FILENAME} !-f # and not a file
RewriteRule ^([a-z0-9._\-]+)$ hotsite/index.php [L]
This, however, should not affect a root URL request i.e. http://mywebsite.com/ because your regex clearly matches one or more characters after / because of the []+ plus sign.
#+FollowSymLinks must be enabled for any rules to work, this is a security
#requirement of the rewrite engine. Normally it's enabled in the root and you
#shouldn't have to add it, but it doesn't hurt to do so.
Options +FollowSymlinks
#Apache scans all incoming URL requests, checks for matches in our .htaccess file
#and rewrites those matching URLs to whatever we specify.
#to enable runtime rewriting engine
RewriteEngine On
#this tells mod_rewrite to leave the URL unchanged (the dash part -) and quit
#other rewwrite processing rules if the requested segment has one of those
#prefixes (that's what we asking when we use the ^ sign), on the list. If the
#prefix is in the list, all subsequent Rewrite Rules are skipped.
#So to avoid some files OR directories to be redirected we use:
RewriteRule ^(somefile|somedir|someotherfile) – [L]
1) Don't we need a condition here before these previous RewriteRule ?
#this will allow direct linkage to this extensions, regardless the case sensitive.
RewriteCond %{REQUEST_FILENAME} !\.(js|ico|zip|rar|mov|mpeg4|mp4|gif|jpg|png|css|doc|pdf|docx|wmv|mpeg|avi|mpg|flv|ppt|pptx|txt)$ - [NC]
2) Will the [NC] flag deal with all case variations here?
#if the request uri has not public...
RewriteCond %{REQUEST_URI} !^/public/.*$
#rewrite to public something...
RewriteRule ^(.*)$ /public/$1
3) Why do we have $1 and not /public/index.php on our RewriteRule?
#rewrite all requests that start with public, to public/index.php and if that's
#the case, don't run any other rule.
RewriteRule ^public/.*$ /public/index.php [NC,L]
4) since this is the last rule, can we remove the L flag ?
No, anything that starts with somefile, somedir or someotherfile will not be rewritten (-). [L] ensures that further rules (including this one) will not be processed.
Yes.
Because the RewriteCond can match /public/otherthing as well, not just /public/index.php.
No, without [L], an infinite loop will occur because the rewritten URL /public/index.php matches `^public.*$.