mod_rewrite and dynamic pages - .htaccess

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.

Related

Rewrite URL with three dynamic parameters

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]

URL Rewriting not working?

im new here.
Im trying to rewrite some urls on my site only for some reason no matter what I try I cannot get them to work!
My directory on my server has the following...
index.php
user-profile.php
.htaccess
On my index.php there is a number of users that all have a view more details button that links through to user-profiles and posts an ID using GET method as such...
http://mysite.com/user-profile.php?userID=2&firstName=Martin&lastName=FAM
However Id like to format them as so...
http://mysite.com/people/2/Martin/FAM
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
Can anybody give me some reason as too why this isnt working?
The regex should be
^people/([^/]*)/([^/]*)/([^/]*)$
You need to make sure you have this option set in your document root directory in Apache's httpd.conf file:
AllowOverride All

Friendly URL and remove php prefix

i have this in my htaccess
Options +MultiViews
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ dash.php?p=$1
RewriteRule ^([a-zA-Z0-9]+)/$ dash.php?p=$1
and im trying to make this url:
http://mywebdomain.com/dash.php?p=dash&mode=All
look like this:
http://mywebdomain.com/dash/All
Is there a way to do this?
and also if i had for example this:
http://mywebdomain.com/user/manage.php?p=manage&mode=Me
how could i get that to look like this:
http://mywebdomain.com/user/manage/Me
I have successfully managed to get the .php extension removed by the Options +Multiviews command but i am struggling trying to work out why it wont work for the re-write of the extended parts of the url.
Thanks for any help in advance too :)
Jonny
With MultiViews on, you do not need Rewrite to accomplish this.
since dash.php file exist in document root, Apache will pass requests to /dash/anything to dash.php, and that php script can parse the URI (found in $_SERVER["REQUEST_URI"]) to check what was the full uri called.
same goes for /user/manage.php ...

.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.

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