htaccess redirect issue mod rewrite - .htaccess

hey well I'm trying to change the url structure using the htaccess file and I'm rewriting my old structure to the new one, although I'm having some issues.
I'm using the following code
RewriteRule video-(.*)-(.*).html?$ http://mysite.com/download-$2-video-$1.html [R=301,L]
Now the problem I'm running into is that when I try to redirect something like
http://mysite.com/video-1-this-is-the-title.html
it's redirecting it to
http://mysite.com/download-this-video-1.html
instead of
http://mysite.com/download-this-is-the-title-video-1.html
if anyone could help that would be great! :)

If the first backreference is always going to be numbers, you could try changing your regular expression to:
RewriteRule video-([0-9]+)-(.+).html?$ http://mysite.com/download-$2-video-$1.html [R=301,L]

Related

htaccess is working but does not replace the url

I'm trying to modify the subdomain name in the URL to make it look nicer. My current URL look something like:
www.mystore.com/productInfo.php?cPath=11_11&productID=222
So, I want to make it nicer by Rewrite in .htaccess in main with this code:
RewriteEngine On
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]
When I run and test it on the URL by typing www.mystore.com/productInfo/11_11/222 in the URL it works well. However, when this page is redirected by a different page or is 'refreshed' with a self redirecting a href= link(in which the link is written in php by the previous programmer), the above old link is still visible in the URL instead of the new one.
I am still a beginner while I suspect that I might need to change something in the cPanel/Apache(I think) for this but currently, I am still do not have access to the cPanel control. Is there anything that I might have missed to write in the .htaccess file or I really do need the cPanel control or any other reasons?
Any help is appreciated. Sorry that I could not find similar questions on this.
You can use the following :
RewriteEngine On
RewriteBase /
#redirect /productInfo.php?cPath=foo&productID=bar to /productInfo/foo/bar
RewriteCond %{THE_REQUEST} /productInfo\.php\?cPath=([0-9_-]+)&productID=([0-9_-]) [NC]
RewriteRule ^ productInfo/%1/%2? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]

.htaccess RewriteRule remove some url parts

I'm still new in htaccess and I want to learn it if possible but I need to finish my project first in school.
My original url is:
http://localhost/example/php/admin/
I want to rewrite it so it will only shows and also that's what will be need to write in url to access the url above:
http://localhost/example/admin/
is it possible with htaccess?
also if you have a good advice where to learn htaccess, I want to learn if possible. thanks :)
Try this
RewriteRule ^php/(.*)$ /$1 [L,NC,R]

how to rediect form htaccess file?

I want to redirect from http://www.example.com/index.php?abz_xyz to http://www.example.com/abz_xyz with htaccess. I am using this code on my htaccess file Redirect /index.php?abz_xyz http://www.example.com/abz_xyz/ but this is not working kindly please help me
Something like this (my regex may be off)
The [L] means Last, so once the rewrite engine hits that line, it'll redirect immediately if it matches
RewriteEngine On
RewriteRule ^index\.php\?abz_xyz$ /abz_xyz/ [L]

URL Rewriting using mod_rewrite

i'm trying lear to make a simple url rewrite and can't understand why is not working.
I created the file .htaccess with this:
RewriteEngine On
RewriteRule ^section\.php$ /how-does-it-work/ [R]
All i want to do is /section.php be displayed as /how-does-it-work/
i'm following a tutorial but doesn't seem to work.
Nothing seems to work… i even tried a mod_rewrite generator, i'm using GoDaddy, is there anything i should activate or something? 'cause so far i'm only dealing with the .htaccess file.
Thanks
You are using them the wrong way around. Try:
RewriteRule ^how-does-it-work/?$ /section.php [L]

htaccess RewriteRule not processing if not all parameters are passed

Hoping someone can help me with this issue...
I have a RewriteRule set up in my htaccess file that looks like this
RewriteRule ^/?find/(.+)/?in/(.+)/(.+)/ /page.html?&type=$1&city=$2&state_abbrev=$3 [L,QSA]
The rewrite works fine if I have a URL that contains all the parameters specified for example: http://www.site.com/find/doctors/in/dallas/tx/
The issue is, I would like to have this rewrite work even if one of the parameters is missing. For example, if I only entered http://www.site.com/find/doctors/ I would still want it to redirect to 'page.html' and just not have the parameters completed. So in that case it would be writing to http://www.site.com/page.html?type=doctors&city=&state_abbrev=. Currently, if I type in a URL that doesn't have all parameters in it the RewriteRule will not work. Any ideas how to fix this issue?
Thanks in advance...
Replace your rule with this:
RewriteRule ^/?find/([^/]+)(?:/in/?([^/]*)/?([^/]*)/?)? /page.html?type=$1&city=$2&state_abbrev=$3 [L,QSA]
There is nothing stopping you from having multiple rewrites. I have many in my .htaccess for different things and many times you have to. Not one rewrite will work for all occasions.
You could try this.
RewriteEngine on
RewriteRule ^find/(.*)/in/(.*)/(.*) page.html?type=$1&city=$2&state_abbrev=$3&%{QUERY_STRING} [L]
RewriteRule ^find/(.*) page.html?type=$1&%{QUERY_STRING} [L]
It will first look for the full URL
http://www.site.com/find/doctors/in/dallas/tx
Then forward it to the first Rewrite rule.
But then if you used this
http://www.site.com/find/doctors
It will skip the first rule and it will match the 2nd rule redirecting to just the partial query string.
Hope that helps but you can make as many rules or conditions you want. And the order in which they come does matter.

Resources