In a website I found, they use friendly URLs like this:
Real URL:
example.com/index.php?mode=product
FRIENDLY URL
example.com/view/product.html
In the friendly URL, there is a feature to get a variable with $_GET function. So if the URL looks like this:
FRIENDLY URL 2
example.com/view/product.html?id=10&lang=en&cur=1
This is similar to the friendly URL but allows me to easily access variable parameters.
Can someone help me write an .htaccess rewrite rule like this?
The [QSA] directive in mod_rewrite is your friend here. It will append all the other query string parameters onto the end of the rewritten url:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^view/(.*).html /index.php?mode=$1 [QSA]
That is untested, but just a quick answer that should get you going.
Related
I am trying to redirect my blog post to specific URL https://example.com/detailblog.php?my-title&s=9 to https://example.com/my-title/9
RewriteEngine on
RewriteRule ^/blog/([0-9]+)\.php /blog/detailblog/.php?s=$1
RewriteEngine on
RewriteRule ^/blog/([0-9]+)\.php /blog/detailblog/.php?s=$1
it's not working.
I would recommend reading beginner guides for URL rewriting like: https://aloneonahill.com/blog/url-rewriting-for-beginners/ and using test tools like: https://htaccess.madewithlove.be/.
It seems to me you have not understood the syntax, based on the example you provided.
I am trying to change my long url to something short I did it successfully but after sending get info URL is not chanining to what I want like I search for something and it show my url like this
/search.php?q=a&limit=150&siz=any
so I want to convert it automatically in url bar like this
/car/150/any
I am using this
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /search.php?q=$1&limit=$2&siz=$3 [R,NC]
but it is not converting link automaticly however it works when I write in url like
/car/150/any
same related question is asked by someone here so I need something like that for my link
Search url automatically in mod_rewrite version
Any help would be appreciated. Thanks
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^search/([^_]+)/([0-9]+)/([^_]+).html$ search.php?act=$1&limit=$2&siz=$3 [L]
access url with
search/car/150/any
better put constant ^search as page identifier to avoiding conflict with other url rewrite,
([^_]+) can receive string input
([0-9]+) only can receive number input
I am wondering how would I change the url from:
single.php?id=1
to:
quote-1
Each submitted data has an id, of course. They can go to the URL, the id of the "quote" to get the quote. How would the URL rewrite look? I've tried to use tools online, but ether they didn't work or I didn't understand how to use it.
Place the following your .htaccess file:
RewriteEngine on
RewriteRule quote-(\d+) single.php?id=$1 [QSA]
mod_rewrite documentation
http://abc.com/test/batch/profile/anyname
In the above url anyname varies depending upon the selected link.
I have to redirect the above url to friend.php and I should pass anyname as parameter to that file
How can I do this?
Put this in your .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^profile/([a-z0-9]+)$ friend.php?username=$1 [NC,L]
This allows that people access the following url on your site:
http://www.yoursite.com/profile/abc
http://www.yoursite.com/profile/abc123
http://www.yoursite.com/profile/123
However, any other characters than alphanumerics will not qualify for the rewrite above.
Edit:
If you actually will be keeping the http://www.yoursite.com/test/batch/-url, the .htaccess would have to be adjusted accordingly. I just assumed that you'd be using the root of http://www.yoursite.com/.
You need to do something like this:
RewriteRule ^/test/batch/profile/([^/?]*)$ friend.php?param=$1
I am not sure if this is possible, but I have the following URL that I want to make more friendly:
http://www.myurl.com/content.php?id=13089093057550&slug=this-is-a-nice-url
And I want it to look like this:
http://www.myurl.com/this-is-a-nice-url
How do I do this?
You'll need to pass the id to your PHP code in order to display content I believe. For example, look at the Stack Overflow URL for this question: http://stackoverflow.com/questions/6520671/htaccess-rewrite-friendly-urls where it is passing both id and slug. So you can have friendly URLs like:
http://www.myurl.com/13089093057550/this-is-a-nice-url
If you decide to go by my suggestion then here is the code you will need in .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^([0-9]*)/(.*)/?$ /content.php?id=$1&slug=$2 [L,QSA]
You can get the slug into the querystring, but without providing the id somewhere it's impossible for Apache to supply it. Hopefully you have a way to get the id out of your database using only the URL slug parameter.
RewriteEngine On
# We don't know the id
RewriteRule (.*)$ /content.php?slug=$1 [L,QSA]