specific redirect required using mod_rewrite - .htaccess

I have to do a very specific url redirect using mod_rewrite within an .htaccess. Below is a url which has to map to the url below it:
m.example.com/123456/123456-product-name/
This needs to map to the following:
m.example.com/product-name/123456
I'm still getting to grips with regex and url rewrites and I've spent a good couple of hours trying to get this right. Can anybody help!?
Thanks in advance

You can do that. in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^\d+/(\d+)-([^/]+) /$2/$1 [R=301,L]
Or just [L] (and not [R=301,L]), if you do that without redirection.

Related

How to redirect url to another with the hash in the middle of the address

I would like to redirect that url:
mydomain.com/folder1/folder2/
to:
mydomain.com/#/folder1/folder2
The thing is, that the /folder1/folder2/ part of URL I want to read as a parameters so that is why I am using hash for it.
So it should be something like:
hostdomain.some/different text/another one
to:
hostdomain.some/#/different text/another one
so I can get the different text/another one as a parameters.
I've tried most of the solution given here, but neither helped.
[EDIT] Ok, I think I found a resolution to this.
Solution!
You can use this rule in you root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/.+)$ /#$1 [L,NE,R=302]
Change 302 to 301 once it is working.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/?$ /#$1/$2 [L,R=301]

htaccess, rewrite multiple pages to the same page but keep original value somewhere

I have been fiddle farting around with htaccess and RewriteEngine but I can't quite get my head around it...
I'm building a website on which I want users to be able to go to /portfolio/typography for example. But I don't want to create seperate pages for each category in this portfolio and thus I want to rewrite (redirect?) all the requests that go to /portfolio/ to the index.php of this directory and load the appropiate projects for this category from there.
Any ideas on how I could do this? I used this to redirect all the requests to /portfolio/:
RewriteCond %{REQUEST_URI} !/portfolio/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /portfolio/ [R=302,L]
Thanks in advance,
Cas Cornelissen
EDIT
Maybe I should note that I have another .htaccess file in the root of my website.
Ok, so I found the answer to my own question...
Seems like the following is working:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

rewrite rule is not redirecting properly

I would like to be able to use rewrite rules to redirect anyone who opens the link :
domain.com/name
to
index.php?username=name
what would be the best way to do it?
I tried to post the htaccess code I wrote but stackoverflow keeps saying it doesn't meet standards so I removed it.
thanks in advance
Something like this will do it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* index.php?username=%1 [L,QSA]
It will map silently
http://domain.com/name
To
http://domain.com/index.php?username=name
For this to work, /name must be the first directory in the incoming URL path.

URL rewritten but indexed as Query String

I tried googling this but all it gave me was tutorials on how to rewrite so I thought I'd give it a shot asking it here.
The problem is that we have a htaccess file that rewrites the urls to a query string:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+\/*[^\/]*\/*.*)$ index.php?app_route=$1 [QSA,L]
This works and it seems google is picking it up, however when you click on the link in google it shows the query string that it's rewritten to. We only link to the 'nice' urls so we don't know where google picked this up. Google for site:sorellehaarmode.nl to see for yourself.
Does anyone have experience with, or a solution to this problem?
Try adding these rules (anywhere to your current htaccess file, below the rules you already have is fine):
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?app_route=([^&\ ]+)&?([^\ ]*)
RewriteRule ^/?index\.php$ /%1?%2 [L,R=301]
This should permanently point the URLs with query strings to the ones without.

Rewriting URL using .htaccess and mod_rewrite

I used this code in my .htaccess to rewrite my urls which is like this:
www.example.com/subcategory.php?subcat=my-test
to
www.example.com/my-test
.htaccess code I used:
RewriteEngine on
RewriteCond $1 !^(subcategory\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ subcategory.php?subcat=$1 [L,QSA]
My problem is whenever I access the url not pertaining to my subcategory.php file like contactus and aboutus, the .htaccess file will somehow put me to subcategory.php file which is not what I want. I want my contactus be handled by contactus.php and aboutus with aboutus.php.
I know that there is something wrong with my .htaccess file but I couldn't fix it by myself for I am not so familiar with the .htaccess coding.
Any suggestion would be greatly appreciated.
Thank you very much!
You are rewriting all requests to subcategory.php. There is no way for .htaccess to tell whether /xxx is a subcategory or some different page, so you could redirect it to a different script.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Use this code snippet instead of yours and move all the "routing" logic to PHP. In PHP, you can find out whether /xxx is a subcategory, a contact page, an article or something else and use a script suitable for that kind of database record.
You will find the requested URL in $_SERVER['REQUEST_URI'].

Resources