.htaccess rewrite dynamic url problems - .htaccess

I know there is lots of rewrite topic but i coulndt get it. So I try to rewrite url with htaccess. But I don't know very well about rewrite rule so i want your helps.
rewrite index.php to root(it works).
www.example.com/index.php ---> www.example.com
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]
After index.php to root rewrite my dynamic url change from this
https://example.com/index.php?baslik=dynamic-slug-url
to this
https://example.com/?baslik=dynamic-slug-url
Php codes(fetch etc.) work fine and no problem. But I want to change into this(I tried dynamic url rewrite generator but no result):
https://example.com/dynamic-slug-url
After the above things, I want to change something else. From this;
https://example.com/specificfile.php?target=dynamicid
to this
https://example.com/dynamicid
When it became upper thing,
https://example.com/specificfile.php?target=dynamicid&baslik=dynamic-slug-url
Will be
https://example.com/dynamicid&baslik=dynamic-slug-url
But I want like this
https://example.com/dynamicid/dynamic-slug-url
I hope i told my problem and other things clear. I tried lost of url rewrite generator but couldnt get no result. Thank you for your answer and good explanation.

I used that problem as a little exercise for myself and this is what I came up with:
# 1) from https://example.com/index.php?baslik=dynamic-slug-url
# to https://example.com/dynamic-slug-url
# and
# 2) from https://example.com/specificfile.php?target=dynamicid
# to https://example.com/dynamicid
RewriteCond %{QUERY_STRING} ^(baslik|target)=([^/.&]+)$
RewriteRule ^(.*)(index|specificfile)\.(php|html?)$ https://example.com/%2? [R=301,L]
# 3) from https://example.com/specificfile.php?target=dynamicid&baslik=dynamic-slug-url
# to https://example.com/dynamicid/dynamic-slug-url
RewriteCond %{QUERY_STRING} ^target=([^/.&]+)&baslik=([^/.&]+)$
RewriteRule ^(.*)(index|specificfile)\.(php|html?)$ https://example.com/%1/%2? [R=301,L]
Demo on https://htaccess.madewithlove.be

Related

htaccess rewrite for known URL and query string

Have been struggling with a 301 redirect issue and cannot see the wood for the trees. Really hope someone can assist.
I have over 400 old urls that we need to redirect to a new urls. I have however modified the htaccess to create a clean url on the new site and am not sure if this is causing an issue or if I am doing something else wrong.
To clean urls in the new site I have added the following to the htaccess fle:
RewriteEngine On
RewriteRule ^([0-9]+) category?cat=$1 [NC,L,QSA]
This is so that instead of www.mysite/category?cat=1 I can have www.mysite/1 and-add-other-information
I am now trying to do the redirects for the 400 urls such as:
RewriteRule ^category?cat=1 1/and-add-other-information [R=301,NC,L,QSA]
RewriteRule ^category?cat=2 1/and-different-information [R=301,NC,L,QSA].
I know the L flag makes a difference and have tried this with and without.
I hope this makes sense.
QueryString isn't part of match in pattern of a RewriteRule.
You need to use RewriteCond directive and match against %{QUERY_STRING} variable like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=1$
RewriteRule ^category$ /and-add-other-information? [R=301,NC,L,QSA]

.htaccess url rewrite rule to subdirectory not working

I want this: RewriteRule ^paper$ /express/index.php?c=p [L] but it does not work. Instead, it's trying to send me to /index.php?c=p, ignoring the subdirectory express altogether. However, RewriteRule ^express/paper$ /express/index.php?c=p [L] works. This is my .htaccess code:
RewriteEngine on
RewriteBase /
RewriteRule ^paper$ /express/index.php?c=p [L]
RewriteRule ^express/paper$ /express/index.php?c=p [L]
The url I WANT is just http://site.com/paper but I have to do http://site.com/express/paper to get the rewrite rule to work.
And yes, I only use one of those rewrite rules at a time.
UPDATE
Well, I'm not really getting any responses, so I'm going to go ahead and close this browser tab. I'll check back now and then but don't be offended if it takes a little while. Thanks for any help offered.

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

modrewrite website with slash in .htaccess

How can I translate an URL like:
http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
to:
http://localhost/mySite/OFFERS/ARTICLE/DETAIL
if one parameter is empty it should still work like this:
localhost/mySite/?link=OFFERS&sublink=ARTICLE
localhost/mySite/OFFERS/ARTICLE
Extra problem: There is an enter page under index.php and the rewrite should work with index2.php. Best would be if it would work under localhost and on live system without changes.
Currently I'm using: RewriteRule ^([^/.]+)$ index2.php?link=$1 [L]
But that only works for one parameter and I couldn't improve this code for ages ^^
RewriteRule ^([^/.]+)(/([^/.]+))?(/([^/.]+))?$ index2.php?link=$1&sublink=$3&subsublink=$5 [L,QSA]
Note that localhost/mySite/OFFERS/ARTICLE links to localhost/mySite/?link=OFFERS&sublink=ARTICLE&sussublink= and not localhost/mySite/?link=OFFERS&sublink=ARTICLE.
Should not be a big issue, but make sure the PHP code doesn't us isset($_GET['subsublink']).
Try adding the following to your htaccess file in the mysitedirectory of your site.
RewriteEngine on
RewriteBase /mysite/
# rewrite http://localhost/mySite/OFFERS/ARTICLE/DETAIL to
# http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
#assumes that index2.php is in the root directory of site
RewriteRule ^([-a-zA-Z0-9])+/([-a-zA-Z0-9])+/([-a-zA-Z0-9])+ index2.php?link=$1&sublink=$2&subsublink=$3 [NC,L]
#redirect index to index2
#if you do not want to redirect, just server rewrite, remove the R=301 below
RewriteRule ^index\.php$ index2.php [NC,L,R=301]

htaccess URL Rewrite for products doesn't redirect me

There is plenty of information out there but nothing I've read on the interwebz has given me an answer as to why my htaccess is not working.
I cannot determine why my rule isn't rewriting the URL as I thought it would. I have the following url:
domain.com/Book/bookpage/index.php?bookID=123&bookName=foo_bar
I would like to change it so that when someone hits that URL, it shows like:
domain.com/Book/123/foo_bar
I started off trying to get it to work using just the Book ID and haven't even gotten that to work.
This is what I have thus far:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2
However, after placing that htaccess in the root of the site and going to the URL:
domain.com/Book/bookpage/index.php?bookID=123
The URL in the address bar remains the same.
try this:
RewriteEngine On
RewriteBase /
# this rewrite domain.com/Book/123 or domain.com/Book/123/
RewriteRule ^Book/([0-9]+)/?$ /Book/bookpage/index.php?bookID=$2 [L,NC,QSA]
# this rewrite domain.com/Book/123/title or domain.com/Book/123/title/
RewriteRule ^Book/([0-9]+)/([a-z0-9\-_]+)/?$ /Book/bookpage/index.php?bookID=$1&bookName=$2 [L,NC,QSA]
Try adding [L,R=301] at the end of the line:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2 [L,R=301]

Resources