htaccess 301 redirect for multiple urls - php

I have many urls such as:
mydomain.com/location/illinois/
mydomain.com/location/wisconsin/green-bay/
mydomain.com/location/new-york/
I have removed the word location and replaced it with:
/blog/category/
so those urls should now be:
mydomain.com/blog/category/illinois/
mydomain.com/blog/category/wisconsin/green-bay/
mydomain.com/blog/category/new-york/
I am trying to get this done with one rewrite rule. But the one I created is not working. Can you tell me what is wrong with it?
Here is what I tried:
RewriteEngine On
RewriteBase /
RewriteRule ^/location(/.*|)$ /blog/category/$1 [L,NC,R=301]

Nice try, you just need to drop the opening slash which is not included in .htaccess matches:
RewriteEngine On
RewriteRule ^location(/.*|)$ /blog/category$1 [L,NC,R=301]
No need for the RewriteBase either. And you had doubled up the slash before the category which you're already capturing.

Related

Using htaccess modrewrite for friendly urls gives a 404 error

I am trying to rewrite
http://example.com/category/this-is-my-category
to...
http://example.com/category.php?id=this-is-my-category
My .htaccess file is below:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^category/(\d+)/([\w-]+)$ /category.php?id=$1 [L]
This gives a 404 error
http://example.com/category.php exists on the server
I have also tried
RewriteRule ^category/(\d+)/([\w-]+)$ ./category.php?id=$1 [L]
and
RewriteRule ^/category/([a-zA-Z0-9]+)$ /category.php?id=$1
I have read some articles on this and can't see an issue with the code in the .htaccess file.
Right, so your first regex...
^category/(\d+)/([\w-]+)$
requires a number between category and the last part, eg /category/1234/something-else.
Your second regex...
^/category/([a-zA-Z0-9]+)$
has an incorrect leading slash (rewrite rules start at the rewrite-base) and requires only letters and numbers after category, eg /category/thisIsMyCategory.
The URL you're testing has letters and hyphens.
To me, it looks like you want
RewriteEngine On
RewriteRule ^category/([\w-]+)$ /category.php?id=$1 [L,QSA]
Demo ~ https://htaccess.madewithlove.be?share=8c3de5aa-68f3-5ec0-9c69-23ff2dbe2d6e
Some notes...
It's rare to ever need RewriteBase, especially if your .htaccess file is in the root directory so I've removed it
I've added the QSA flag so any query parameters are preserved. For example
/category/this-is-my-category?foo=bar
becomes
/category.php?id=this-is-my-category&foo=bar

Redirect entire sub-directory to root directory

So I am trying to clean up my search engine redirect errors. I had some old development sites that got indexed that I want to redirect to the main site.
Basically I want everything in the /dev/ folder to go to https://myfakewebsite.com/
I added this to .htaccess file in the /dev/ directory:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ /$1 [R=301,L]
I tried this but it doesn't quite work, it does take out the /dev/ but it keeps the rest of the link there.
For example: https://myfakewebsite.com/dev/index.php?route=product/category&path=122
redirects to: https://myfakewebsite.com/index.php?route=product/category&path=122
I want it to redirect to: https://myfakewebsite.com/ (removing the /index.php?route=product/category&path=122 part).
Is there a way to do this with .htaccess?
You can use the following rule in your /dev/.htaccess
RewriteEngine on
RewriteRule (.*) / [L,R=301]
The rule above redirects all requests from /dev to the root of site ie . /dev/* => / including query strings from /dev?querystring to /?querystring . By default mod-rewrite appends old query string to the destination path so if you do not want it then you can use QSD flag in your rule or you can another rule to handle and remove QUERY_STRING .
You can use the following rules to redirect urls with and without query string to / . The ? at the end of the rule's destination discards the old query string.
RewriteEngine on
# redirect /dev?querystring to /
RewriteCond %{QUERY_STRING} .+
RewriteRule (.*) /? [L,R=301]
# redirect /dev/uri to /
RewriteRule (.*) / [L,R=301]
Make sure to clear your browser cache before testing these redirects.
You might want to achieve this kind of "trivial" redirect with the RedirectMatch directive from mod_alias, instead of doing this with mod_rewrite.
RedirectMatch 301 "/dev/(.*)" "/$1"
See documentation at https://httpd.apache.org/docs/2.4/mod/mod_alias.html.
You left the match results as part of the redirect on this line:
RewriteRule ^(.*)$ /$1 [R=301,L]
Specifically $1 as part of /$1 (the redirecting portion of the rule). Since this file is within the /dev directory it automatically removes /dev/ then applies the match to the redirect. Since you are using the capture ($1) as part of the rule, it'll take everything it matched after /dev/ (in your rule you should read it as ^/dev/(.*)$) and apply it to /$1, where $1 is whatever it "found" during ^(.*)$ in the previous step.
Try removing the pattern match, e.g.:
RewriteRule ^(.*)$ / [R=301,L]
Additionally, since you aren't using the results of the match you can remove the parenthesis (e.g. ^.*$). You can also put this .htaccess file in the root and simply redirect ^/dev(/?.*)$ to / depending on how you want to manage your subdirectories.

htaccess not working for directory redirecting

I have directory my-site.com/m/ and it contain so many sub-folder so i want internal redirect for sub-folder to my-site.com/m/req="rest-path-of-directory"
ex
my-site.com/m/article/pc to my-site.com/m/?req=article/pc
For this I use the code below but something is wrong, it is not working for me.
My .htaccess file is :
RewriteEngine On
RewriteRule ^/m/([A-Za-z0-9\/]*)/?$ http://www.localhost/m/index.php?req=$1 [L]
Try
RewriteEngine On
RewriteRule ^/m/([A-Za-z0-9\/]*)$ http://www.localhost/m/index.php?req=$1 [L]
I think the /? outside your capture group brackets is never being matched, as all the / characters are being captured inside your ([A-Za-z0-9\/]*) expression.
For internal redirection you need to remove the hostname and scheme from the Rewrite target
Try :
RewriteEngine On
RewriteRule ^m/([A-Za-z0-9\/]*)/?$ /m/index.php?req=$1 [NC,QSA,L]

url rewriting with .htaccess

I'm trying to rewrite the url on a site I have on localhost (port 8000), I have already set this up for certain other directorys but I can't manage to do it for this one. I compared it to several other working .htaccess files but there wasn't any difference. (The module is activated.) The site is in a subdirectory called HTF and the file single.php works just fine. The htaccess file is placed in the same directory.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule videos/([0-9]+)/$ /HTF/single.php?id=$1 [L]
I would like to get an url like localhost:8000/HTF/video/1232434 for exemple.
Any help appreciated, tested several types of htacces files, here a working exemple for a other subdirectory called sorted:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^image-post/([0-9]+)/(.+)\.php$ /sorted/image-post.php? id=$1&title=$2 [L]
RewriteRule ^pages/([0-9]+)/(.+)\.php$ /sorted/page.php?page_number=$1&cat=$2 [L]
Your rule will match videos, not video as you were wanting. Also it will only match with a trailing slash.
This will work
RewriteEngine on
RewriteRule ^video/([0-9]+)/?$ single.php?id=$1
localhost:8000/HTF/video/1232434
will not match
videos/([0-9]+)/$
because of the trailing slash /. Try /? to make the slash optional or leave away. And videos.
^/HTF/video/([0-9]+)/?$ /HTF/single.php?id=$1 [L]
or
^/HTF/video/([0-9]+)$ /HTF/single.php?id=$1 [L]
This shall do the job. Sorry can not try here.

Rewrite Rule for GET variables .htaccess

Well I'm havin trouble creating .htaccess, anyway I have categories in my menu for example test1,test2,test3 that are the names of a folders, and what I'm trying to do is to point the second parameter of the url to index.php, for example http://myapp/test1 I want to be able from the index.php to have $_GET['cat'] = test1 and so on..
Here's what I've tried so far and didn't work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)/$ index.php?cat=$1 [NC,L]
Any help with this? Much appreciated.
Try removing the trailer / character from your rewrite rule. ^(.*)/$ matches any URL that ends in a / like http://myapp/test/. You are probably looking for ^(.*)$ or to strip any leading slash ^/(.*)$. Note though that if index.php is accessed directly via f.ex. http://myapp/index.php this would also match and rewrite to http://myapp/index.php?cat=index.php and any images wouldn't also be directly accesible. So you might want to add some RewriteCond that first checks if the requested URL is an existing file with RewriteCond %{REQUEST_FILENAME} !-f before the RedirectRule to rewrite the URL.

Categories