How to remove duplicate directory from url in htaccess? - .htaccess

I have URLs like this:
domain.com/gedenkportal
domain.com/gedenkportal/gedenkportal
domain.com/gedenkportal/gedenkportal/schiermeyer-heinrich
and I would like to shorten them to:
error 404 / don't allow access to that file
domain.com/gedenkportal
domain.com/gedenkportal/schiermeyer-heinrich
I don't want anyone to be able to access the old "/gedenkportal" directory (before the redirect)
My current code is:
RewriteRule ^gedenkportal(/.+)$ /$1 [R=302,L,NC]
But that one doesn't work at all. Can anyone please help me out on this? :-)

Related

RewriteEngine: Ignore all query strings

I would like to ignore all query-strings in my redirect. How would I achieve this?
Basically, /project/(.*) should always redirect to /?tag=project&id=$1
/project/100?shareid=fromsomeemailprovider
should end et the ? and only redirect to Id 100.
Thank You!
I tried the following:
^/project/([^/]*)[/]?(.*)$
/?tag=project&id=$1&$2
to put the query-string behind another &, but this only works if the first URL hast a / at the end of it, which it often hasn't and the RewriteRule can't detect the ? sadly.
With your shown samples, please try following htaccess rules file.
RewriteEngine ON
RewriteRule ^(project)/(.*)/?$ index.php?tag=$1&id=$2 [QSA,NC,L]
Please make sure:
You clear your browser cache before testing your URLs.
You make sure to keep your index.php file along with your htaccess rules file. Also add / in case both of these files are in root location in your system.

Redirect the URL using .htaccess file

I have a page showing the products with the hyperlink for it as
www.domainname.com/productname
now my client needs to add store and needs the URL to show as
www.domainname.com/store/productname
I have done it via code and now when I click on it for a detail page, its still redirecting to
www.domainname.com/productname
but need to be
www.domainname.com/store/productname
tried with this:
RewriteRule ^store/?$ domianname.com/?$ [NC,L]
in .htaccess file, not sure whether I'm on page
Can any one tell me how to do it via .htaccess file.
Your RewriteRule is backwards: you need the path you're matching first, then the path you're redirecting to. Try this:
RewriteRule !^/store/(.*) /store/$1 [NC,L]
Also, you don't actually need mod_rewrite to do this. You could try mod_redirect, which is simpler and easier to understand:
RedirectMatch !^/store/ /store/
(N.B. I haven't tried either of these, so I'm not 100% certain they do what you want.)

Htacces rewrite rules and conditions

I have this rule which works perfectly
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+) /demo/index.php?category=$1&subcategory=$2
this converts my link from
/demo/index.php?category=ABC&subcategory=XYZ
to
/demo/ABC/XYZ
but at times I want to enter just the category name like this
/demo/ABC/
But unfortunately this gives error Page not found. What causes this error and how can I fix it?
Also can someone provide link to RewriteEngine resources/books/tutorials. I couldn't find anything that would help a beginner like me. Thank you.
Change your rule to:
RewriteRule ^([^/]+)/([^/]*) /demo/index.php?category=$1&subcategory=$2 [L,QSA]
This making second argument optional (using * instead of + in [^/]*)
Unless /demo/ABC/ is a directory that exists on your local file system, it won't be able to find the page. Your first rule is only rewriting that parameter-filled link into that pseduo-path, it doesn't establish a virtual directory or create a solid redirect.
The best info for RewriteRule is probably going to be in the actual Apache docs.

url rewrite don't redirect to the correct page

I have the following rule in .htaccess file:
RewriteRule ^cat-([0-9]+)\.html$ ad_list_cat.php?category_no=$1
the ad_list_cat.php file is in a sub folder /contents/. I also tried
RewriteRule ^cat-([0-9]+)\.html$ /contents/ad_list_cat.php?category_no=$1
when I enter the url 'http://localhost/cat-1.html' it gives error 404.
I think the problem with sub-folder.
whats wrong in my rule?
Regards:
Because it is looking for your ad_list_cat.php in your root dir.
Please read a little about re-write rules BEFORE you start asking questions.

Use htaccess to mask folder name

Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>

Resources