Redirect subirectories without affecting the main directory - apache

While updating things in WordPress I found that plugins that generate slugs can't use a slug that's already in use by another page so I have had to change the slug to be something slightly different. I now need to make sure that anyone that tries the old urls get redirected to the new one without affecting the main page.
So I have /members/ with a list of members and you can click members to go to /member/[name] I need to redirect /members/[name] to the /member/[name]
( [name] can be anything so it needs to be a wildcard.)
I have used both of the following htaccess rules (not at the same time) but they always end up redirecting the /members/ page also which breaks everything.
RewriteRule ^members/(.*) http://domain.tld/member [R=301,L]
RedirectMatch 301 ^/members/.*$ http://domain.tld/member
What am I missing to make it only redirect if there's something after the /
Sidenote: I've tried finding an answer to this but all the results I have found are trying to do the exact opposite (redirecting only the exact match of /members/) that I need and wont work.

You are not using the captured part of the request.
Try :
RedirectMatch ^/members/(.+)$ /member/$1

Try this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Redirect 301 /oldDir/old.php http://yourDomain/newDir/new.php

Related

Htaccess Redirect URL with two forward slashes (not double) won't work

I want to redirect from one domain to a new domain. At the same time, the URL structure has changed.
Old: https://www.olddomain.com/parentpage/oldtitle/
New: https://www.newdomain.com/newtitle
This is wordpress, and I placed this code above the Wordpress stuff, as well as tested it here: https://htaccess.madewithlove.be/
I tried this, which doesn't work:
Redirect 301 /parentpage/title https://www.newdomain.com/newtitle
Also, when testing it at https://htaccess.madewithlove.be/, I do have this redirect:
Redirect 301 /parentpage https://www.newdomain.com/parentpage
The tester would skip my preferred redirect above, and use this one, leaving me with this, which does not exist:
https://www.newdomain.com/parentpage/oldtitle
Even when I place the preferred redirect above this one. I need both, unfortunately.
Have also tried the following RewriteRules (not all at the same time)
ReWriteRule https://www.olddomain.com/parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule /parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule "https://www.olddomain.com/parentpage/oldtitle/" "https://www.newdomain.com/newtitle"
I think it has something to do with that second forward slash separating the parentpage name and page title, but I can't figure out how to fix it.
In RewriteRule it wouldn't match http or https in it, you may try following.
please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/parentage/oldtitle/?$ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/newtitle [R=301,L]

How to rewrite url and redirect with apache mod rewrite?

I have got url:
ipaddress/panelname/main/index.php
How to rebuild it to
ipaddress/center/index.php
?
ofcourse we can see another pages, not only index.php, but this folders in url we can see forever.
I tryed to do this in .htaccess:
RewriteEngine on
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]
RewriteRule ^/panelname(.*)$ /center$1 [QSA,L,R=301,NC]
Redirect 301 ^/panelname(.*)$ /center$1
but i don't see redirect from panelname to center.
but if i type center all works good (but i don't shure, that it works good by my htaccess or by symlink, which i was created in filesystem)
How to rewrite all to another links and howto see redirect from old links to my new? Thank you.
RewriteRule in directory context (which .htaccess is), does never begin with a slash, because the common prefix is stripped from the matched portion first.
Redirect does match strings, not regex'es. The variant that works on a regex is RedirectMatch. Both only work on absolute URL's (the one beginning with a slash).
You either have to do the following:
RewriteRule ^panelname(.*)$ /center$1 [R,L]
or:
RedirectMatch 302 ^/panelname(.*)$ /center$1
Change [R] to [R=301] once you have tested that EVERYTHING works. If you choose the second option, only change 302 to 301 after testing that everything works.
If you want to show /center/index.php to your visitors and keep a redirect from old URL to this URL then you will need one redirect and one rewrite rule (that you already have).
RewriteEngine on
# external redirect from old URL to new one
RewriteCond %{THE_REQUEST} /panelname/main/(\S+) [NC]
RewriteRule ^ /center/%1 [R=302,L]
# internal forward from new URL to actual one
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]

How to simulate directories with a htacess file (mod_rewrite)?

what I try to do is to simulate directories with the help of a htaccess file.
I have a website with a file like this:
http://www.domain.com/filename.php?t=yeah-a-title-2014
Now, I would like to rewrite the URL above to the following:
http://www.domain.com/directory1/yeah-a-title-2014/
If a visitor enters one of the two URLs, he should see the second one in his address bar but the content of the filename.php?t=yeah-a-title-2014 should be displayed.
I have no idea how to realize this.
Any ideas?
This is better known as SEO-urls (search engine optimized), SEF-urls (search engine friendly), fancy urls and a couple more of those terms. The basic problem with these kind of constructions, is that they cause an infinite loop if not implemented correctly, and therefore usually the THE_REQUEST trick is used, because %{THE_REQUEST} is always equal to the request, even if the url is rewritten, which in turn prevents the external redirect from matching if the internal rewrite matches.
#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /filename\.php\?t=(.*)\ HTTP
RewriteRule ^ /directory1/%2/ [R,L]
#Change [R,L] to [R=301,L] after ALL rules do what you want them to do, and before your site goes live
#Internal rewrite
RewriteRule ^directory1/([^/]+)/?$ /filename.php?t=$1 [L]

Replace one URL with another by using mod_rewrite

Current situation
RewriteRule ^$ /index.php?page=Portal [R=301,L]
When a user comes to the website and goes to the "root" url of the domain (RegExp "^$") he's redirected to /index.php?page=Portal
That's working.
Now we have "index.php?page=Portal" in the google index and we have tons of links to that page on various locations all over the internet.
Intended new situation
We want the portal page to show up on the root url - no redirect. That's no problem... Just remove the redirect:
RewriteRule ^$ /index.php?page=Portal [L]
Now we also want the old url to redirect to the new location, and that's where I fail but can't see why:
RewriteCond %{QUERY_STRING} ^page=Portal$
RewriteRule ^index.php$ http://www.jacatu.de/? [R=301,L]
As soon as I do this I end up in a redirect loop:
(When I change to 302 in .htaccess I see 302 redirects, so the loop really seems to be caused by mod_rewrite)
But why? All rules are marked as last [L] - so I think I can rule out that rule 2 triggers rule 1.
I enabled logging as suggested by Jacek Prucia and in fact it looked like having [L] in the URL doesn't stop execution. Both rules were processed.
I now changed the first rewrite to
RewriteRule ^$ /index.php?page=Portal&int=1 [L]
so that it doesn't match the RewriteCond of the internal rewrite so theoretically my problem is solved. It would be nice to know, though, why it did what it did. :)

.htaccess redirect doesn't hide url

my .htaccess in the root folder includes the following lines :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]
when I open the address http://example.com/bla.htm, my browser doesn't hide the GET values specified in the .htaccess, it redirects me to ?city=bla. eventhough I'm not using the [R] switch. This has always worked for me before (as I remember, haven't dealt with htaccess in a while). What's wrong here ?
When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).
Assuming both URLs are on the same server, you need to do something like
RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]
Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.