Simply remove a url segment with .htaccess - .htaccess

I am simply trying to get this URL:
http://foo.com/entry/random-entry-123
to redirect to:
http://foo.com/random-entry-123
The "random-entry-123" is dynamic. Different for each entry.
Any help with this is greatly appreciated!

Assuming no further rewrites are in use, and all links inside /entry/ are to rewritten, then try this:
RewriteEngine on
RewriteBase /
RewriteRule ^/entry/(.+)$ /$1 [L,QSA]
Lose the [L] if there are further rewrites ahead in the file.

Although this has already been answered, it didn't work for me when I had two segments after the first, eg:
http://foo.com/entry/random-segment/random-entry-123
Instead this worked for me and also takes care of 301 redirects:
RedirectMatch 301 /entry/(.*) /$1
Hope this helps.

Related

Replace part of a URL with .htaccess

I had to change the name of a subpage and now the problem is that all shared links on Facebook, Twitter, etc. are not working anymore.
That's why I am trying to redirect only a part of a URL with .htaccess but I have no solution yet.
It should work like this:
www.mydomain.com/feeds/details/ --> www.mydomain.com/highlights/details/
I hope you can help me!
It will depend on your server but you are looking for something along these lines
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
//301 Redirect Entire Directory
RedirectMatch 301 /feeds(.*) /highlights/$1
You can try rewriting the URL using RewriteRule, like follows:
.htaccess
RewriteEngine on
RewriteRule ^/feeds/details$ /highlights/details/
Hope, that works for you.
You can find more information here.
I took your tip and adjusted my reality.
My problem:
https://example.com/contact/example.com
My solution:
RedirectMatch 301 (.*)/example.com(.*) $1/$2
After solution, redirect:
https://example.com/contact/

301 redirect .htaccess parameter php?fr_xyz to php?en_xyz

1st i have to say: i tried google of course. so many tips about my request - but i dont get it. maybe you can help...
it sounds simple: i want a 301 via .htaccess to a another parameter file
for example:
www.mydomain.tld/ runs without .htaccess to www.mydomain.tld/index.php?de_xyz
but:
what i want is, if you call www.mydomain.tld you get to www.mydomain.tld/index.php?en_xyz
-> ?de to ?en
if i try a simple: Redirect 301 /index.php http://www.mydomain.tld/index.php?xyz i get a redirection error on this side.
i have tried so many ways. dont get it :/
thx for your answer
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)en_xyz(&|$) [NC]
RewriteRule ^/?$ %{REQUEST_URI}?en_xyz [L,QSA,R=302]
Have you tried
RewriteRule ^/?$ /index.php?en_xyz [R=301]
Note that 301 is a permanent redirect so the browser might not re-read your configuration if you redirect to a wrong address by mistake while trying.
If not working you might want to add
RewriteLog "/some/path/rewrite.log"
RewriteLogLevel 3
to your vhosts file if you have access (it might not work in .htaccess) and get the details from log.
By the way I'd prefer handling this in index.php:
header('Location: index.php?en', true, 301);

Use part of static URL to redirec to dynamic URL using .htaccess

Usually is the other way aroud, but I need to redirect my static urls to dynamic ones as a variable part of the first one.
I need to redirect:
http:/example.com/sub1/sub-2/2014147-test-page-45x.html
http:/example.com/sub1/sub-6/8014149-test-24page.html
http:/example.com/sub1/sub-25/7514157-58test-page.html
to
http://example.com/sub1/testing.php?s=2014147
http://example.com/sub1/testing.php?s=8014149
http://example.com/sub1/testing.php?s=7514157
I know I need to use %{REQUEST_URI}, but I can't put it together.
Any help will be appreciated.
Thanks.
Assuming your .htaccess is at root of example.com, adding following to it will do the trick.
RewriteEngine On
RewriteBase /
RewriteRule ^sub1/[^/]+/(\d+).*$ sub1/testing.php?s=$1 [R,L]
This should work:
RewriteEngine on
RewriteRule ^sub1/(.*)/([0-9]+)-(.*)$ /sub1/testing.php?s=$2 [R=301,L]

.htaccess Removing ampersand from URL?

I have been banging my head against a wall for a long time trying to figure out how to get rid of the last part of some of the URL's on my site. For example, I would like to rewrite this:-
http://www.mysite.com/335-protective-wrapping&page=prod
to this
http://www.mysite.com/335-protective-wrapping
There are about 2000 URL's with &page=prod at the end of them which I need to remove. Here's some more example URL's
http://www.mysite.com/335-protective-wrapping&page=prod
http://www.mysite.com/455-bubble-bags&page=prod
http://www.mysite.com/150-specialist-tapes&page=prod
I have tried many solutions but haven't come up with anything that works.
Any help would be much appreciated.
Thanks.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^([^&]+)&page=prod$ /$1 [L,R=301]
This should work:
RedirectMatch 301 ^/(.*)&page=prod$ http://www.mysite.com/$1
If you really want to take into account the query part (after the ?) of the source-URI you have to use RewriteCond plus RewriteRule, you cannot just use RewriteRule.
(http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond)
Sorry, anubhava, that wont work for that reason.

.htaccess Rewrite Questions

I need to redirect a dynamic url to another url, with the dynamic portion intact, for example:
http://www.mydomain.com/VARHome.aspx?var=32
needs to redirect to:
To: http://www.otherdomain.com/VARHome.aspx?var=32
The value after "var=" is dynamic, it can be any number.
This needs to be done via .htaccess.
Thanks for any help!
I'm thinking RewriteRule ^(.*)$ http://www.otherdomain.com/$1 [R=301,L] or something along those lines should do the job.
Edit: Maybe something like
RewriteRule ^VARHome.aspx?var=(.*)?$ http://www.otherdomain.org/VARHome.aspx?var=$1/ [R=301,L]
if you are in mydomain.com, write in .htaccess
RewriteEngine on
RedirectMatch 301 ^/VARHome.aspx?var=(.*)$ http://www.otherdomain.com/VARHome.aspx?var=$1

Resources