I have an old php dynamic web site that uses index.php to generate everything. I changed the index.php file to product.php and made keyword rich url using product.php. The keyword riche works. Now i need to make a 301 redirect in the htaccess so that the indexed links will be redirect to product.php
This works
# Rewrite keyword-rich URLs
RewriteRule ^product/(.*)/.*-([0-9]+)\.html$ product.php?lang=$1&cat=$2 [L]
# Rewrite keyword-rich URLs
RewriteRule ^product/(.*)/.*-([0-9]+)/.*-sc([0-9]+)\.html$ product.php?lang=$1&cat=$2&subCat=$3 [L]
but i tried to add before these rules this but it is not changing the url
RewriteCond %{THE_REQUEST} ^index\.php?(.*)$ [NC]
RewriteRule ^product.php?$1 [R=301,L]
I am using mamp to do my testing
Try this instead (before your existing rules):
RewriteCond %{THE_REQUEST} \ /+index\.php\?(.*)$ [NC]
RewriteRule ^ product.php [R=301,L]
Note the \ before the ? in the condition, then the space between the ^ and product.php. The query string will automatically get appended. You don't need to backreference it.
Related
I have a url like this:
http://www.localhost.com/code_category/computers/
I want to change this url to:
http://www.localhost.com/category/computers/
I don't need url redirection.
My current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You only want to redirect code_category to categoryexternally and keep the path as it is internally so, try this :
RewriteCond %{THE_REQUEST} !\s/+category/ [NC]
RewriteRule ^code_category/(.*)$ category/$1 [R=302,L,NE]
RewriteRule ^category/(.*)$ code_category/$1 [L]
The above will redirect any request containscode_category/whatever to category/whatever externally and keep the internal path as it is .
If you want only request contains code_category/computers/ change it to this :
RewriteCond %{THE_REQUEST} !\s/+category/computers/ [NC]
RewriteRule ^code_category/computers/(.*)$ category/computers/$1 [R=302,L,NE]
RewriteRule ^category/computers/(.*)$ code_category/computers/$1 [L]
test it , if it is fine change 302 to 301 for permanent redirection.
Note: clear your browser cache then test it.
.htaccess file
Add this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost.com [NC,OR]
# without redirect
# RewriteRule ^/code_category/computers/$ category/computers/
RewriteRule ^/category/computers/$ code_category/computers/
# redirect method
# RedirectMatch 301 ^/code_category/computers/$ category/computers/
RewriteEngine On enables mod_rewrite.
RewriteCond %{HTTP_HOST} shows which URLs we do and don't want to run through the rewrite.
In this case, we want to match example.com.
! means "not." We don't want to rewrite a URL that already includes folder1, because then it would keep getting folder1 added, and it would become an infinitely long URL.
[NC] matches both upper- and lower-case versions of the URL.
RewriteRule defines a particular rule.
The first string of characters after RewriteRule defines what the original URL looks like. There's a more detailed explanation of the special characters at the end of this article.
The second string after RewriteRule defines the new URL. This is in relation to the document root (html) directory. / means the html directory itself, and subfolders can also be specified.
For Reference click here
Hope this helps!
Redirect If url contains particular word without changing it.
eg: http://localhost/APIs/student
should redirect to "http://localhost/APIs/"
I tried many things one of them given bellow:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^[A-Z]{10,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([a-z0-9-]+)/?$ /APIs/index.php?page=$1-10 [QSA,L,NC]
above snippet fails if I pass more than one parameter in the link after slash.
Here are the rules
RewriteEngine on
RewriteRule ^APIs/(.*)$ APIs/index.php?page=$1 [NC,END]
If you request for
http://localhost/APIs/student/test
you will be internally redirected to (you will see the same url as above)
http://localhost/APIs/index.php?page=student/test
In the index.php file use $_GET["page"] to get student/test and you can decide what to show in the page based on this.
I am using pretty url for my project and it is working fine.
http://testurl.com/user/12345
I am using .htaccess for redirection.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ user/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)&name=([^&\s]+)\s [NC]
RewriteRule ^ user/%1/%2? [R=301,L]
RewriteRule ^user/([0-9]+)/?$ user.php?id=$1 [L]
RewriteRule ^user/([0-9]+)/([^/]+)$ user.php?id=$1&name=$2 [L]
Today i found if i change the link in browser like this http://testurl.com/user.php then page is also loading i want to show error message (Alert) if someone directly trying to access
The best way is to add your error or redirect in the user.php without id query string.
But you also can add (after RewriteBase /):
RewriteCond %{THE_REQUEST} \s/user\.php\s [NC]
RewriteRule ^ - [F]
You might as well solve your problem through code reorganization. I did this in one of my projects, and it has worked well.
1. When you create a pretty URL, move the according file into another directory
So, in this case, you had the URL example.com/user.php?id=123 visible externally. Now, you want a pretty URL for it, e.g. example.com/user/123.
On file level, before you had
- user.php
I suggest you move that to another directory, where all scripts live which are accessed by pretty URL only:
- rewrites/
|- user.php
2. Create redirects for your old URL to your new URL, externally
The same as you did above.
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ user/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)&name=([^&\s]+)\s [NC]
RewriteRule ^ user/%1/%2? [R=301,L]
3. Rewrite the new, pretty URL to moved script, internally
The same as you did above, with difference that the directory name rewrites is added.
RewriteRule ^user/([0-9]+)/?$ rewrites/user.php?id=$1 [L]
RewriteRule ^user/([0-9]+)/([^/]+)$ rewrites/user.php?id=$1&name=$2 [L]
4. example.com/user.php now fails with a 404
Because /user.php does not exist anymore in the file system, it automatically fails with a 404 if called without params.
5. Benefits
This approach might sound like additional work for nothing, but these are the benefits making it worthwile in my opinion:
You do not need an additional .htaccess rule for error handling
You get better code organization
You get better overview of what is already accessible with pretty URL
Hi I have a website eg website.co.uk
I would like a htaccess file using the rewriterule to make the urls better
Current URL
Website.co.uk?campaign=holiday&affiliate=1
the parameters (holiday & no 1) will change, depending on where the inbound link comes from
sometimes there will only be 1 parameter which would be the campaign like below
Website.co.uk/holiday
sometimes there will be 2 parameters
Website.co.uk/holiday/100
I have get variables in my php to get the variables on an index.php page
$campagin = $_GET['campaign'];
$affiliate = $_GET['affilaite'];
The first one has only 1 parameter I.e their might not be an affiliate
Or the May be 2 parameters I.e a campaign and an affiliate
the page they are redirecting to is index.php
In my php it checks if the gets are set but just struggling with rewrite rule
I totally understand your concern, and there is this site, where you could randomly create your .htaccess file based on your requirements.
http://www.htaccessredirect.net/
Give it a try!
In the htaccess file in your document root:
RewriteEngine On
# for redirecting query string URLs to nicer path URLs
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)\?campaign=([^&]+)&affiliate=([0-9]+)
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)\?campaign=([^&]+)
RewriteRule ^ /%1? [L,R]
# internally rewrite path URLs back to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([0-9]+)|)$ /index.php?campaign=$1&affiliate=$2 [L]
I have an external backlink which is linking incorrectly to my website.
They are adding /%E2%80%8E to the end of the link so it is coming in as http://mywebsite.com/%E2%80%8E.
I want to use htaccess to redirect these people to my homepage.
This is what I currently have:
#This version does not work for some reason
RewriteRule %E2%80%8E https://mysite.com [B,R,L]
RewriteCond %{QUERY_STRING} %E2%80%8E
RewriteRule .? https://mysite.com [B,R,L]
# This version works if I type in the DECODED version of the string
RewriteRule ‎ https://mysite.com [R,L]
RewriteCond %{QUERY_STRING} ‎
RewriteRule .? https://mysite.com [R,L]
Thanks
If you don't want to use the decoded string, you can use \x##. The reason why the decoded string works is that in RewriteRule's, the URI is decoded before the pattern is applied.
RewriteRule ^\xE2\x80\x8E$ / [L,R=301]
Give this a try in your htaccess.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/%E2%80%8E\s [NC]
RewriteRule ^ https://mysite.com/ [L,R=301]
You could solve this problem without using .htaccess rewrite. On some of my website I do check, either in the header of the page (with PHP or JS) or in a custom 404 page.
In my opinion this method is slightly better than mod rewrite just because it doesn't require you to have the mod_rewrite module enabled on the server.