I am using rewrite rules to make my URls SEO friendly and the following rule is working fine for products page.
RewriteCond %{QUERY_STRING} ^pid=([a-z0-9A-Z_\-]*)$
RewriteRule ^([^/]+)/([^/]+)$ product/viewproduct.php?pid=%1&cat1=$1&cat2=$2 [L]
However, when I pass additional query parameters to product page (e.g. utm_source), It's returning a 404.
Please help.
Your RewriteCond blocks to access to the RewriteRule.
Remove the $ to access to your page and, add ,QSA to access to your parameters.
RewriteCond %{QUERY_STRING} ^pid=([a-z0-9A-Z_\-]*)
RewriteRule ^([^/]+)/([^/]+)$ product/viewproduct.php?pid=%1&cat1=$1&cat2=$2 [L,QSA]
Related
I wonder how to redirect all URLs in .htaccess that do not have a parameter to another URL.
Example:
Any access without "&tag=xxx" redirects to index.php?tag=xxx
You can use this rule at top of your site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)tag=xxx(?:&|$) [NC]
RewriteRule ^(index\.php)?$ %{REQUEST_URI}?tag=xxx [L,NC,R=302,QSA]
Negative RewriteCond %{QUERY_STRING} checks whether query parameter is NOT present and RewriteRule adds the missing query parameter and does a full redirect.
Here is the rules:
#RULE REDIRECTQUESTIONSURL9
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteCond %{QUERY_STRING} ^scid=9
RewriteCond %{THE_REQUEST} questions.php
RewriteRule ^questions\.php$ /general-knowledge_questions-answers_national-famous-day-0 [R,L,QSD]
These types of links are not working
> http://www.example.com/questions.php?scid=6
> http://www.example.com/questions.php?scid=8
There are two issues that immediately come to mind when looking at your rewrite rules.
scid=9 is looking for the static value 9. Neither of shown URLs have that value.
!^POST$ the URL you're showing is making a GET request.
Give this a try:
#RULE REDIRECTQUESTIONSURL9
RewriteCond %{REQUEST_METHOD} !^GET$
RewriteCond %{QUERY_STRING} ^scid=\d+
RewriteRule ^questions\.php$ /general-knowledge_questions-answers_national-famous-day-0 [R,L,QSD]
Also do you care about the parameter coming from the query string? If not you could just take that requirement off. If so you should capture it and append it to the rewritten URL.
You should change your links to
action="general-knowledge_questions-answers_national-famous-day-<?php echo $id;?>"
or
href="general-knowledge_questions-answers_national-famous-day-<?php echo $id;?>"
then have your htaccess work the opposite way. The rewrite rewrites the request on the backend; the frontend URL stays the same.
So
RewriteCond %{REQUEST_METHOD} !^GET$
RewriteRule ^general-knowledge_questions-answers_national-famous-day-(\d+) questions.php?scid=$1
I use the following rules to rewrite a subdomain call to a page in the root of the website:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9/_.-]+)\.domain\.com$ [NC]
RewriteRule .? headlines.php?url=%1 [L,QSA]
This works fine. I use this for a rss - news related website. For example: http://economy.headlines.com will internally look at http://www.headlines.com/headlines.php?url=economy
I also want to link to the news items in the following way:
economy.headlines.com/news/title/id
How do i do this ? Because every time the first rules are "fired". Even if i make other rules with the [L] flag the other rules are fired and nothing happened.
How can i combine the rules above with new rules which also look at files in the root of the site but with parameters in the url ?
You should be able to combine both if you evaluate the path component of the request you rewrite:
Rewriteengine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9/_.-]+).domain.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ headlines.php?url=%1&category=$1&title=$2&id=$3 [L,QSA]
Probably you have to do some fine tuning, but I think you get the idea: the first argument of the RewriteRule is a regular expression that splits the request path into its components. Those can be referred to using the $1 and $2 notation you can see towards the end of the rule.
Edit: added the category parameter to the RewriteRule as discussed in the comments below.
I have a two part problem that I have only been successful with the first part.
I have the following listed in .htaccess which works great:
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
So visitors can go straight to mysite.com/senior and the correct internal page (demo.php?dID=1) gets pulled up.
My problem is that I also would like a rewrite where /demo.php?dID=1 shows up in the URL bar as /senior. So existing links show up with the new user friendly url.
My attempt so far has been:
RewriteCond %{QUERY_STRING} ^dID=1$
RewriteRule ^demo.php$ senior [NC]
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
Thanks for your time and help.
You want to match against the request instead of the query string and redirect the browser:
RewriteCond %{THE_REQUEST} \ /demo\.php\?dID=1($|\ |&)
RewriteRule ^demo.php$ /senior? [NC,R]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Place this additional rule before your current rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+demo\.php\?dID=([^\s&]+) [NC]
RewriteRule ^ /senior? [R=302,L]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Your current rule based on QUERY_STRING will loop since internal rewrite rule will populate the QUERY_STRING and both rule will keep triggering each other.
I'm trying to set 301 redirects on pages that have 'page=1' in the URL to stop duplicate content issues.
e.g.
http://www.domain.com/reviews/?page=1
to
http://www.domain.com/reviews/
I've tried all of the variations I can find and can't seem to get anything to work.
RewriteRule ^reviews(/)?page=1$ http://www.domain.com/reviews/ [L,R=301]
RewriteRule ^(.*)/?page=1 http://www.domain.com/reviews/ [R=301,L]
RewriteCond %{QUERY_STRING} page=1
RewriteRule ^reviews$ http://www.domain.com/reviews/ [R=301,L,NE]
None of these have worked. I'm not really sure what else to try.
There are multiple different sections of the site that I need to do this for:
reviews
news
videos
accessories
hardware
An overall solution to redirect all ?page=1 URLs to their relevant section would be best.
Use this code to redirect every URI with ?page=1 to one without the query parameter:
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Or else if you want to redirect ONLY /reviews URI then
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^(reviews)/?$ /$1? [R=301,L]
the question is already answered, i just would like to mention that your rules are not working because you didn't append a trailing ? to the new url in the rewrite rule