httacces Rewrite URLs - .htaccess

I wanted to rewrite some urls from
www.example.ro/women_shoes.php to www.example.ro/women-shoes/
and with pagination looks like
www.example.ro/women_shoes.php?page=1 to www.example.ro/women_shoes/pagina-2/
www.example.ro/men_shoes.php to www.example.ro/men-shoes/
with pagination like the first one
www.example.ro/sandale_barbati.php to www.example.ro/sandale-barbati/
and so on..
for the last example i tryed
RewriteRule ^sandale-barbati/pagina-([0-9]+)/ /sandale-barbati.php?page=$1 [L]
RewriteRule ^sandale-barbati/?$ /sandale_barbati.php?$
but i need to write that for every page...
is there a way to do that automaticaly for all pages ?
thanks

If you always use the same naming for all your pages, this will work:
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/pagina-([0-9]+)/$ $1_$3.php?page=$4
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/$ $1_$3.php
RewriteRule ^([a-zA-Z_]+)/pagina-([0-9]+)/$ /$1.php?page=$2
RewriteRule ^([a-zA-Z_]+)/$ /$1.php
You can surf to /women_shoes/ and it will be the /women_shoes.php page.
You can surf to /women-shoes/ and it will be the /women_shoes.php page.
You can surf to the /shoes/ and it will be the /shoes.php page.
I hope this has met all your requirements and was of sufficient help to you. For future reference, the website http://www.regular-expressions.info/ is very handy when dealing with RegEx (such as .htaccess rewrite rules).

Related

htaccess URL rewrite pattern format

I've had a good look through all the other htaccess url rewrite questions, but all of them deal with the reverse of my problem.
The site I am working on takes content from child pages in WordPress and presents them as anchored sections on the parent page. The problem for the site now is that if Google (or the built in search, though that can probably be done in the templates) links to one of the child pages it will direct users to the single page rather than the correct section of the parent page.
I was hoping to come up with a URL rewrite pattern which would reformat the URL to what I need, but it doesn't seem to work... and I know that's because I have done it wrong!
The URL which needs to be rewritten is something like:
website.com/parent-page/child-page/
I need this to be rewritten to:
website.com/parent-page/?subpage=child-page
My initial stab at it looks like this, but I know I've misunderstood something about how to format the pattern.
RewriteRule ^/$1?subpage=$2 ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$
Can anyone help format this correctly or point out where I'm going wrong?
You can use:
RewriteEngine on
# If the request is not for a valid file/directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/?subpage=$2 [QSA,L]
No initial / in htaccess RewriteRule first uri
And optional final /
RewriteRule ^([^/]+)/([^/]+)/$ /$1/?subpage=$2 [L]
The htaccess rewrites didn't seem to work, so I've created a PHP redirect instead by taking the page slug, page parent permalink and mashing the two together.
Not the most elegant solution, I expect, but it works.

htaccess redirect trouble

I use mod-rewrite and I do this:
RewriteRule ^brand/([A-Za-z0-9-]+)/([A-Za-z0-9-\.\_]+)/?$ model.php?ref=$2&brand=$1 [NC]
and it works great! But I have a problem: when you go by url like
brand/stuff/ffgfgfgfg.php it will redirect to a page brand/stuff/ffgfgfgfg?ref=ffgfgfgfg.php&brand=stuff. I want to get rid of such a situation as it is not very good for the promotion on Google.
Maybe anyone can help me out and suggest a solution to redirect that awkward page?
Well here are your options. If, as you say in your comment (although I find it a bit strange) you want to redirect brand/stuff/ffgfgfgfg.php to brand/stuff/ffgfgfgfg, you can use
RewriteRule ^brand/([A-Za-z0-9-]+)/([A-Za-z0-9-\.\_]+).php/?$ brand/$1/$2 [NC]
Apparently, if you want to redirect to brand/stuff/ffgfgfgfg?ref=ffgfgfgfg&brand=stuff, you could use
RewriteRule ^brand/([A-Za-z0-9-]+)/([A-Za-z0-9-\.\_]+).php/?$ model.php?ref=$2&brand=$1 [NC]

mask URL (with parameters) with htaccess

i need to mask a long, stupid php URL with a relatively simple URL with htaccess.
For example, i need to mask -
http://mysite.com/index.php?this=that&this=that&whatevs=this&that=&id=5
with this simple URL -
http://mysite.com/mypage.php
I know php can do the stuff very easily by fetching the content of that page and displaying it. But in my conditions, there are some problems with ajax calls and some other things.
Moreover, i prefer to use htaccess. I did some research but htaccess codes are very confusing to me.
I took the liberty of removing the .php part from mypage.php as it has no real use, and makes the url look less pretty.
RewriteEngine On
RewriteBase /
RewriteRule ^mypage$ /index.php?this=that&this=that&whatevs=this&that=&id=5 [L,QSA]
So you only want redirect http://mysite.com/index.php?this=that&this=that&whatevs=this&that=&id=5 to http://mysite.com/mypage.php?
Then use this RewriteRule:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} this=that&this=that&whatevs=this&that=&id=5
RewriteRule ^index.php$ http://mysite.com/mypage.php? [L,R=301]

htaccess mask for my url

I'm trying to mask a part of my url using .htaccess but I'm having several issues
my url is
http://ristorantitalianetwork.com/ristorantitalianetwork.com/admin/
I'd like to remove the duplicate ristorantitalianetwork.com, so I'd like that my url will be like
http://ristorantitalianetwork.com/admin/
I've used
RewriteRule ^ristorantitalianetwork.com/([^/]*)$ ?q=$1 [L]
but it doesn't work
Could you please help me to figure out how to solve this problem?
Thanks a lot
Best regards
You really need
RewriteRule ^/admin$ /ristorantitalianetwork.com/admin [L]
Bear in mind that the URL you should expose to users is http://ristorantitalianetwork.com/admin/ which will then internally get converted (rewritten) to http://ristorantitalianetwork.com/ristorantitalianetwork.com/admin/.
It is not the other way round as many believe.
You almost did it! But...
In your question, your rewriterule says that it is applied on URLs that don't end with a slash (/). And you say you want to rewrite some URLs... and give URLs examples with a slash (/).
If you need to do a real redirect (i.e. the URL in the browser changes):, here's the good rewriterule:
RewriteRule ^ristorantitalianetwork\.com/([^/]*)/$ /$1 [QSA,R=301,L]
If you need to do only internal redirect:
RewriteRule ^ristorantitalianetwork\.com/([^/]*)/$ /$1 [QSA,L]
Keep in mind that the URL must end with a slash (/).

SEO Friendly URL Command in .htaccess

I have a dynamic page that looks like the following:
www.sitedomain.com/page.php?id=30&name=about_our_company
I want to make my website links SEO friendly by having something like the following:
www.sitedomain.com/about_our_company.html
Or
www.sitedomain.com/about_our_company
My question is: what regex/code I should have in the .htaccess file?
Thanks
This ofc has a fixed id of 30.
RewriteRule ^about_our_company/?$ /page.php?name=$1&id=30 [NC,L]
since /about_our_company doesn't include the ID, it's impossible to invent the ID correctly.
the way I do it in my own CMS is to have something like this in the .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
then in index.php, use the $_REQUEST['page'] (assuming PHP) variable to find the right page details in the database
You could improve this to be more generic:
RewriteRule ^([^/]+)/([^/]+)?$ /page.php?id=$1&name=$2 [NC,L]
The following URL's would all match:
http://example.com/30/about_our_company
http://example.com/29/contact
http://example.com/10/our_work

Resources