Pretty url not redirecting, htaccess - php

I'am quite new with htaccess. I have successfully created .htaccess file with following rules to redirect user with pretty url.
My actual url
http://localhost/domain.com/Job-Details.php?Job_ID=30
What i want to achieve
http://localhost/domain.com/30
My Current htaccess
RewriteEngine On
RewriteBase /domain.com/
# Get the URI-path directly from THE_REQUEST variable
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(.*)\.php [NC]
# Search friendly URLs for job detail page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [NC,L,R=301]
Issues I'am facing
Redirection with pretty url not happening. I'am stil seeing old url
Using mentioned htaccess when i enter pretty url in address bar directly then page displays fine but url gets changed to my current url automatically.
any idea what wrong i'am doing here.

Please try with the following /domain.com/.htaccess code:
RewriteEngine On
RewriteBase /domain.com/
# Redirect actual URI to SEF URI
RewriteCond %{THE_REQUEST} (?:GET|HEAD)\s\/domain.com\/Job\-Details\.php\?Job_ID\=(\d+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# SEF URI for Job-Details page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+)$ Job-Details.php?Job_ID=$1 [NC,QSA,L]
Change R=302 to R=301 if you are happy with the results. Remember to clear your cache before testing this, otherwise your browser may redirect based on historical cache.

Change this:
RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [NC,L,R=301]
RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [L,QSA]
With R=301 you were creating a 301 redirection.

Related

I am trying to achieve a directory URL using parameters with htaccess

I would like to show certain content on my website. I choose the content based on a URL parameter, like page=about. This works well, and my URL looks like example.com/mysite/index.php?page=about. My issue is that I would like my URL to read example.com/mysite/about, where the last part is the name of the page parameter.
I am attempting to do this through htaccess. This is my exact code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
I see no change on my page nor to the URL structure. The URL still reads ...index.php?page=about. Am I doing something wrong? I tried this a bunch of different ways and I still get the same result. I also tried simply doing this, with no change:
RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1 [NC]
I didn't think it was working, so I tried a redirect to some other site and it worked. Therefore, the htaccess file is getting read and able to function.
Any assistance would be helpful!
Thank you,
BP
You may use these 2 rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(mysite)/index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/mysite/([^/]+)/?$ [NC]
RewriteRule ^ index.php?page=%1 [L,QSA]

Php url rewrite and redirect in htacess

I want to redirect my site to friendly.
I have my website pull data from database and adress is /post?id=1
and i want to change it to /post/1
I already wrote the code for rewrite but i cant make sense from google research how to redirect to /post/1.
my code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L]
Use:
Options +FollowSymLinks
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /post/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L,QSA,NC]
This one worked for me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ post.php?id=$1 [NC]
if you goto url /post/check1/
the htaccess file internally call post?id=check1

How to 301 redirect URL's with "index.php?" in a path?

I am trying to redirect old URLs that look like this:
mysite.com/index.php?dispatch=products.view&product_id=30176
to new URL's that are SEO friendly and look like - mysite.com/category/product
I am using CS-Cart 4.3.1 with SEO add-on enabled, and when I have imported all the products and categories the URLs were automatically changed.
This add-on has 301 redirect functionality in admin, which enables me to create 301 redirects manually. However, something is wrong and it doesn't work with redirects that have "index.php?" in a path to start with.
I have tried redirecting old URLs without "index.php?" in a path - like:
"dispatch=products.view&product_id=30176" to "mysite.com/category/product" and it works, page is being redirected. But this is not the correct URL that is indexed by Google and need redirecting.
I suspect the issue is somewhere in Rewrite Conditions or Rewrite Rules on htaccess file that were created by system upon activation of SEO add-on.
This is how it looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
Options -MultiViews
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
</IfModule>
If anybody can think of a reason why the redirects are not working when "index.php?" is included in a path, I would greatly appreciate your help.
The built-in "301 redirects" feature does not support non-seo friendly URLs as old URL parameter. It means that you cannot use "index.php?dispatch=products.view&product_id=30176", but you can use "old_website_category/old_website_product" one.
To realize what you want, please add 301 redirects to the .htaccess file. E.g.
RewriteCond %{QUERY_STRING} ^product_id=30176$
RewriteRule ^/?index\.php$ http://example.com/category/product? [L,R=301]

PHP Mod Rewrite

I have a .htaccess which does a basic rewrite which looks like the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L,QSA]
The next condition is to remove www. from the URL and looks like this:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
The problem is, when I call a link such as:
http://somesite.com/category/subcategory/?id=123
and add the www. manually, it rewrites the URL to this:
http://www.somesite.com?url=category/subcategory/?123
The page stills load but, I've been told that's terrible for SEO. Any thoughts of how to fix this?
Your current issue is that you have your WWW redirect after your main SEO rules, this is what happens behind the scenes:
You access http://somesite.com/category/subcategory/?id=123
Your rules internally redirect it to index.php?url=category/subcategory/?id=123
Your last rule to redirect without the www will also take place and will ended up redirecting it to:
http://somesite.com/?url=category/subcategory/?id=123
In order to fix that you would need your rules as follow:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L,QSA]
Which leads us to your second issue, you've been using 301 redirects so your browser have cached some redirects and you will need to use a different browser temporarily to test your changes, preferable one you haven't used yet to access that site, while you clear the cache of that browser and wait for it to completely clear out.
Once the cache of your default browser has cleared you can use it as usual and you should get the same response.

Forcing a trailing slash to a URL after a subdomain rewrite

I'm re-writing a subdomain to a 'folder' - actually a page in wordpress, and this all seems to be working correctly. I don't want the address in the URL bar to change though.
Everything works fine unless the user does not put a trailing slash after the page name, then the page is still redirected to the correct URL but the URL in the address bar changes
For example: foo.example.com/bar
Becomes: public.example.com/foo/bar
Where : foo.example.com/bar/ stays at the correct URL in the address bar but shows the redirected page (this is correct)
What rule do i need to add to add in a trailing slash if its not sent?
The code i have so far is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^(.*)$ http://public.example.com/foo/$1 [P]
# rules for WordPress ...
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#####
</IfModule>
Any help would be fantastic, I'm pretty new to htaccess. Thanks!
Phew, after a bit of playing around i seem to have got it working:
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://foo.example.com/$1/ [L,R=301]
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^(.*)$ http://public.example.com/foo/$1 [P]
Basically, the first block adds a trailing slash to the URL is it's not there in the first place, then the second block does the proxy redirect for the URL.
As far as i can see this catches all cases, but let me know if there are any gaping holes!
This rule should do it:
RewriteRule .*[^/]$ %{REQUEST_URI}/
Put this rule in front of your other rules. You also may want to add a condition to exclude existing files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/

Categories