How can I mod_rewrite all incoming URLs except certain specific ones? - .htaccess

I'm trying to rewrite URLs where there are a few fixed page URLs, and everything else gets rewritten. Here's what I need:
domain.com/
domain.com/about
domain.com/* (anything else)
Should redirect to:
/index.php
/about.php
/display.php?id=*
No idea how to do it. Can anyone help me out?

Solved! This works:
RewriteRule ^about$ /about.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /display.php?id=$1 [L]
The index page is handled by using (.+) instead of (.*), which lets the site config forward the request normally to index.php so .htaccess doesn't try and forward it to display.php.

Related

Apache redirect directive not acting as final rule

I am trying to achieve a simple redirect - from /news to /insights
I have the following in my .htaccess file:
redirect 301 /news /insights
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
Whenever I've used the redirect directive before, any matched URLs would be redirected and no further rewrites in the file would be processed. That is to say, going to /news would send you to /insights, and the rewrite to index.php would not be processed.
However, with this current setup, going to /news sends me to /insights?p=news, so for some reason the rewrite to index.php is still being processed.
Furthermore, if I comment out the index.php rewrite, then I get sent to /insights as expected.
This isn't how I've usually experienced this working so am unsure why it's doing this.
I have also tried the following:
RewriteEngine On
RewriteRule "^/news" "/insights" [R=301,L]
This simply results in a 404 instead of redirecting, which I also do not understand.
I am aware I could do the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/news"
RewriteRule ^ /insights [R=301,L]
which does work, however, I don't really want to have multi-line rewrites for lots of URLs, and would like to understand why the other 2 examples do not work.
You just need to insert this rule before last catch-all rule.
RewriteRule ^/?news/?$ /insights [R=301,L,NC]
Place it just below RewriteEngine On line so that mod_rewrite engine executed this rule before other rule.
Make sure to test it in a new browser.

Mod rewrite for replicated wordpress site

I'm trying to direct all traffic to the homepage only to a php script called go.php that gets a variable from the URL.
If someone visits domain.com/username go.php gets the username, looks up their information, saves the information to a session and then redirects to index.php and displays a modified version of the homepage (same domain) that has the retrieved information. Everything works except the mod rewrite part.
I tried the following and am not sure what I am doing wrong:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^/$ go.php?id=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My logic was that if a request is to index.php it should be allowed, to prevent looping.
If the request is to the homepage it will go to go.php?id=username and the that script will redirect to index.php and trigger the prior mod rewrite rule to prevent looping.
Otherwise, it will do the regular redirect to index.php if the directory or filename doesn't exist.
Any thoughts on how to fix this?
I think this is what you mean:
RewriteRule ^(.*)$ go.php?id=$1 [QSA]
Explanation:
I'm looking at the ^/$ in the regular expression in the go.php rewrite rule. I've tested that and it appears to be an impossible match in that situation. One might be wanting to capture requests for root. But the forward slash is not passed to this portion of the RewriteRule for root. so a call for root (only) is ^$. And there's also no capturing parenthesis to feed the $1 you have appended to go.php?id=$1.

Using .htaccess to change directory in url

I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory

Htaccess single page redirect problems

I'm having some issues setting up single page redirects using htaccess. Currently I have a htaccess file with:
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteRule (.*) index.php?path=$1 [QSA,L]
Then a set of redirects e.g.
Redirect 301 /oldpage.htm http://www.mydomain.com/new-page
But the problem I am having is that when I go into a browser and type in the old URL, I get redirected to URL with a parameter attached, e.g:
"http://www.mydomain.com/new-page?path=oldpage.htm"
For some of my redirects this seems to work anyway, for some it produces a 404 error. I've also tried using RewriteCond and Rewrite Rule to write more generic catch all redirects for those pages that I can, and I'm having the same issue.
I'm thinking that some other rule must be interfering with my redirects - the only one I can see which might do so is the rule above, but if that was the case shouldn't the URL I am redirected to end up being
"http://www.mydomain.com/index.php?path=oldpage.htm"
Can anyone explain why parameters are being appended to the URLs and how I can stop this happening so that my redirects work?
Cheers!
This is because mod_alias (the Redirect directive) and mod_rewrite (the Rewrite* directives) are both being applied to the same URI in the URL-file mapping processing pipeline. In order to keep this from happening, you need to stick with one or the other in this case.
You also want the redirect to get applied first:
RewriteRule ^/?oldpage.htm$ http://www.mydomain.com/new-page [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteRule (.*) index.php?path=$1 [QSA,L]
Your index.php rule is going to catch all requests that aren't CGI scripts that are symlinks, so, pretty much everything if that's really your intention. Otherwise you can let legit requests get by unscathed by including:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Above the last rule.
because your $1 in rewrite rule
RewriteRule (.*) index.php [QSA,L] try this

domain regex for .htaccess

Firstly, sorry for my bad English.
I want config my .htaccess to rewrite URL.
example.com/company1.com
instead example.com/sub=company1.com
My .htaccess now:
RewriteEngine On
RewriteRule ^([a-z_]+)/?$ index.php?sub=$1
I was search in stackoverflow.
If i using (.*) regex for all charaters or ([a-z\.]+) for include "dot" character in domain string ( company1.con), my skin was broken.
My temporary solution is use ([a-z_]+) with http://example.com/company1_com instead
http://example.com/company1.com
It's bad solution :(
So, please give me regex for this problem.
Thanks.
Rewriting for Apache is described in mod_rewrite.
For you, as long as you ignore possible GET-parameters or paths, it should be
RewriteEngine On
RewriteRule ^/([^?/]+) /index.php?sub=$1 [L]
I guess it was broken because either you were missing the "/" before index.php, there is a longer path in GET ( example.com/company1.com/css/style.css ) or you submit a form ( example.com/company1.com?a=foo&b=bar ).
You need to prevent the index.php from looping:
RewriteEngine On
# let index.php pass through, thus stopping the rewrite loop
RewriteRule index.php - [L]
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]
You could also do a check for existing resources first. Since index.php exists, that would also break the loop. This would make it so if you're requesting static content like javascript or css, it won't get routed through index.php:
RewriteEngine On
# request isn't for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]
Try this one
RewriteEngine On
RewriteRule ^(.*)$ index.php?sub=$1

Resources