Rewrite URL with three dynamic parameters - .htaccess

I am working on my website where URL is something like:
http://mywebsite.com/series.php?sid=1&part=1&name=Introduction%20to%20HTML
I have tried a rule in my htaccess which is:
RewriteRule ^([^/]+)/([^/]+)/([^\.]+)\.html$ /series.php?sid=$1&part=$2&name=$3
In order to rewrite URL in form of:
http://mywebsite.com/1/1/Introduction to html.html
Unfortunately, it is not working for me.
Please someone help me in sorting out the issue. I would also love it if someone makes me understand how it worked.
I'm newbie for .htaccess.
In above rule, I can see three different parts like:
1st part: ^([^/]+)
2nd Part: ([^/]+)
3rd part: ([^\.]+)\.html$
These three parts are separated by backslash. How they work and how can I utilize the same Rewrite URL form for more than 3 let say 4 or 5 parameters and less than 3 like 2 parameters?
Thank You!

Your rule is correct, just add L flag to end the rule and make sure .htaccess and mod_rewrite are enabled through Apache config.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^\.]+)\.html$ /series.php?sid=$1&part=$2&name=$3 [L,NC,QSA]

Related

erase part of a URL using htaccess rewrite

i need to remove part of Joomla/Virtuemart generated SEF URI using .htaccess
the URI represents a menu hierarchy and structured this way:
online-store
- inner-store
-product-catalog
this is the resulting URI:
www.domain.com/online-store/inner-store/product-catalog
i would like to change it to:
www.domain.com/online-store/product-catalog
thought this might help but its not making any difference
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^online-store/inner-store/\d+-(.+) /online-store/$1 [R=301,L]
i know its not considered good practice but i can't change the menu structure.
any suggestions ?
This regex \d+-(.+) will match 1 or more digits followed by hyphen followed 1 or more any thing
Try this code instead:
RewriteRule ^(online-store)/inner-store/(.*)$ /$1/$2 [R=301,L,NC]
Make sure this is first rule in your .htaccess and use a different browser to test it to avoid caching issues.

mod_rewrite and dynamic pages

I tried following a tutorial on the internet about mod_rewrite but it wasn't really for me. I created a .htaccess file that has the following code for now:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
From what I understand this is the basic setup of .htaccess to rewrite urls, followed by the instructions... what and how to change. I tried different exampled but it didn't worked for me. I have a dynamic page with the url localhost/alpha/oferta.php?id=52042156c65d4, where id="..." is the unique id of that offer. I want to change it to localhost/alpha/oferta/id=".."
Can you please show me an example of how can I achieve that? Also if you know any helpful tutorials let me know. Let me know before downrating so I can edit my question. Thanks!
So you want this kind or URL : localhost/alpha/oferta/id=123abc to be redirected to localhost/alpha/oferta.php?id=123abc.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^alpha/oferta/id=([A-Za-z0-9]+)$ alpha/oferta.php?id=$1 [L]
Remember a few things :
this won't magically change "old" URLs into "new" ones. You must use rewritten ("new") URLs everywhere. Then your htaccess will change this readable URL into a technical one, which can be used by your code.
this redirection is transparent. If you want the URL to change into the browser bar, use [L,R=301] instead of [L].
this will only accept letters (case insensitive) and numbers for your id.
you can find a good cheat sheet about mod_rewrite here.

.htaccess rewrite url with parameter

I want to change my url with only the parameter. As I am passing only one parameter, so I want the sub url should be changed with my parameter name. I want change the url as the following type-
From:
http://www.xyz.com/cat.php?slag=season
to
http://www.xyz.com/season
Can anyone help me to do it. I don't know how to do it. Thanks
Options +FollowSymLinks
RewriteEngine On
RewriteBase /path/to/your/directory
RewriteRule ^(.*)cat/(.*)$ cat\.php?slag=$2&%{QUERY_STRING} [L]
put the above in your .htaccess file.. and that would do the magic..
Note :
RewriteBase /path/to/your/directory
(use this line only if your application is in any of subfolder the folder)
I suggest you obtain a copy of the .htaccess that WordPress uses. It's a quite powerful starting point that allows you to handle routing internally from within your Application - which you might feel more comfortable with, as it seems to me.

Simple url rewrite

I'm trying to rewrite part of an url as I've changed a CMS and still want Google to find my articles.
I have:
www.mywebsite.com/vision
www.mywebsite.com/vision/40/some-article-name
and want to rename them:
www.mywebsite.com/news
www.mywebsite.com/news/40/some-article-name
Any hints as to the re-write rules or where I can look? I'd like to change the rules in my .htaccess file.
# Activate Rewrite Engine
RewriteEngine On
# redirect /vision to /news
RewriteRule ^vision$ http://www.mywebsite.com/news [R=301,NC]
# redirect /vision/bla-bla to /news/bla-bla
RewriteRule ^vision/(.*)$ http://www.mywebsite.com/news/$1 [R=301,NC,QSA]
In theory (and practically) these 2 rewrite rules can be combined, but then if you have URL that starts with "vision" (like this, for example: /visions/hurray) then such rule may redirect wrong URLs. Therefore I have done it via 2 rules which is much safer.
Try: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
or: http://httpd.apache.org/docs/2.2/mod/mod_substitute.html if you want to change links in the html content returned to the browser.
Here is an example of how I might do the rewrite I think you're after...
RewriteRule ^(.)/vision/(.)$ $1/news/$2
This may be to broad of a rewrite scope in which case this may be better...
RewriteRule http://www.mywebsite.com/vision/(.*)$ http://www.mywebsite.com/news/$1
Also learning the basics of regex will be a needed skill for doing any complex rewriting IMO.
Hope that helps.

Why this RewriteRule doesn't work?

I'm developing a web site containing 3 pages (Home, page2, page3) ... in the second page there is a navigation bar, with 4 items (subpage1, subpage2, ...), that I use to replace the content of the page 2 with url variables! In other words, the second item of the navigation bar in page2 points to:
http://localhost/uk/page2/index.php?pg=subpage2
the item 3 point to:
http://localhost/uk/page2/index.php?pg=subpage3
Now I would like to use more friendly urls via .htaccess!
I've written this:
RewriteEngine On
RewriteRule /uk/page2/(.*)/$ /uk/page2/index.php?pg=$1
in the .htaccess placed in the root!
But doesn't work!
Please help!!!
When you're using .htaccess you don't have the leading slash:
RewriteEngine On
RewriteRule ^uk/page2/(.*)/$ /uk/page2/index.php?pg=$1
G'day,
I'd suggest enabling the RewriteLog config option at a high level to check what's actually happening under the covers.
Has AllowOverides been enabled?</obvious> (-:
Seems like you're out of luck using .htaccess
Unbelievably mod_rewrite provides URL manipulations in per-directory context, i.e., within .htaccess files, although these are reached a very long time after the URLs have been translated to filenames. It has to be this way because .htaccess files live in the filesystem, so processing has already reached this stage. In other words: According to the API phases at this time it is too late for any URL manipulations. - Apache mod_rewrite doc.s (emphasis mine)
It may be the trailing slash at the end, so change this:
RewriteEngine On
RewriteRule /uk/page2/(.*)/$ /uk/page2/index.php?pg=$1
to this:
RewriteEngine On
RewriteRule ^(.*)uk/page2(/?)(.*)$ /uk/page2/index.php?pg=$3
Another thing you should check is that you have AllowOverride set to All in your httpd.conf file, instead of None. If it is set to None, you won't be allowed to do anything with .htaccess.

Resources