How to remove part of a URL using .htaccess - .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott

RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]

Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

Related

htaccess rewrite for known URL and query string

Have been struggling with a 301 redirect issue and cannot see the wood for the trees. Really hope someone can assist.
I have over 400 old urls that we need to redirect to a new urls. I have however modified the htaccess to create a clean url on the new site and am not sure if this is causing an issue or if I am doing something else wrong.
To clean urls in the new site I have added the following to the htaccess fle:
RewriteEngine On
RewriteRule ^([0-9]+) category?cat=$1 [NC,L,QSA]
This is so that instead of www.mysite/category?cat=1 I can have www.mysite/1 and-add-other-information
I am now trying to do the redirects for the 400 urls such as:
RewriteRule ^category?cat=1 1/and-add-other-information [R=301,NC,L,QSA]
RewriteRule ^category?cat=2 1/and-different-information [R=301,NC,L,QSA].
I know the L flag makes a difference and have tried this with and without.
I hope this makes sense.
QueryString isn't part of match in pattern of a RewriteRule.
You need to use RewriteCond directive and match against %{QUERY_STRING} variable like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=1$
RewriteRule ^category$ /and-add-other-information? [R=301,NC,L,QSA]

Rewrite http://example.com/test.asp?index=3 to http://example.com/about

I need to Rewrite the old urls generated by ISS to a new system we have build (Joomla).
The url's had to be google friendly. What we want to happen:
Rewrite http://example.com/test.asp?index=3 to http://example.com/about
I've used a few Rewrite's i knew, but they dont work:
RewriteRule ^/test.asp?index=3 / [R=301,L]
RewriteRule ^/test.asp?index(.*)3 / [R=301,L
What pice of code am i missing/doeing wrong?
Kind regards.
You must use QUERY_STRING to check query string.
You can use this code in your htaccess (in document root folder)
RewriteEngine On
RewriteCond %{QUERY_STRING} ^index=3$ [NC]
RewriteRule ^test\.asp$ /about [R=301,L]
Note: put this rule before Joomla's rules

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

htaccess redirect a url with a param and remove duplicates

I have this url
http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com/?page_id=61
and i want to redirect to the root like this
http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com
here is my htaccess
RewriteEngine on
redirect 301 /?page_id=61/ http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com
but nothing is happening...any ideas
Also I was wondering if there is a way also in .htaccess to delete a duplicate /about ...so for example if the url is
http://somesite.com/about/about
it will rewrite the rule to always be
http://somesite.com/about
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page_id=61$
RewriteRule (.*) http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com? [R=301,L]
RewriteCond %(REQUEST_URI) ^/about/about[/]?
RewriteRule (.*) http://somesite.com/about? [R=301,L]
should do it.
I don't know the exact answer off the top of my head as I don't work directly with apache that much any longer, but I think you need to look into apache's mod_rewrite module. Try taking a look at:
Apache's mod_rewrite documentation
This post about blocking certain URLS

.htaccess 301 redirect path and all child-paths

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

Resources