I have read other threads to configure the result below. The problem I'm having is that the url contains the 'local' server as well.
I have a Search URL of:
https://www.example.com/news/?s=recipe
(can be "recipe", "article", "news", "place", etc).
Required output result:
https://www.example.com/news/search/recipe/
Part of the problem is that there will NOT always be a search query parameter available to the News page URL, so will have to incorporate that into the Rewriting code.
## Rewrite the Search s= to an SEO friendly output
<IfModule mod_rewrite.c>
#RewriteBase /
# external redirect from action URL to pretty one
RewriteCond %{THE_REQUEST} /news/\?s=([^\s&]+) [NC]
RewriteRule ^ news/search/%1? [R=302,L,NE]
#
# internal forward from pretty URL to actual URL
RewriteRule ^news/search/([^/.]+)/?$/ news/?s=$1 [L,QSA,NC]
</IfModule>
Is there a better solution?
Related
I am trying to redirect my site using .htaccess if the url contains a certain parameter.
For example when my site contains ?lang=de, then I would like to have the site to be permanently redirected to https://www.example.com/de/
This is what I've tried:
# BEGIN Redirects for mobile params
RewriteEngine On
RewriteCond %{REQUEST_URI} lang=de
RewriteRule .* https://www.example.com/de/
# END Redirects for mobile params
Any help would be appreciated, thanks
UPDATE: This is what I'm trying now, but I'm getting:
# BEGIN Redirects for mobile params
RewriteEngine on
# if the query string contains a parameter called "lang"
RewriteCond %{QUERY_STRING} (?:^|&)lang=de
# then redirect (permanently) to /
RewriteRule ^ https://www.example.com/de/ [L,R=permanent]
# END Redirects for mobile params
But this is resulting in ERR_TOO_MANY_REDIRECTS
UPDATE: I found that when it put this at the end of my .htaccess that it starts to work.
I found that when it put this at the end of my .htaccess that it starts to work.
This may be due to earlier directives that prevent the redirect loop (in other words there is a conflict). Ideally, this redirect should be near the start of the .htaccess file.
However, you should still discard the query string from the redirected response. As it stands, your redirect to /de/ will still contain the lang=de URL parameter. The QSD flag on Apache 2.4 will do this. For example:
RewriteCond %{QUERY_STRING} (?:^|&)lang=(de)
RewriteRule !de/ /%1/ [QSD,R=permanent,L]
The %1 backreference contains the "de" captured in the query string.
I want to convert a URL from
http://localhost/projectname/api/index.php?type=login
to
http://localhost/projectname/api/login
Convert is not a got term here.
The question with htaccess in your case is, what should happen with a request that is coming from a Browser. If you like that a request from the browser looks like http://example.com/projectname/api/login but internally it should do http://example.com/projectname/api/index.php?type=login than this is called rewrite.
The other option is that you want to have a redirect, that means if a browser is requesting e.g. http://example.com/projectname/api/login the server respond with the correct URL e.g. http://example.com/projectname/api/index.php?type=login and the browser now loading this page instant. If you test this in your Browser you will see that the URL will change.
So for a internal rewrite you can use this:
RewriteEngine On
# Rewrite from e.g. /projectname/api/login to /projectname/api/index.php?type=login
RewriteRule ^/?projectname/api/(.*)$ /projectname/api/index.php?type=$1 [QSA,L]
for a redirect you do
RewriteEngine On
# Redirect from e.g. /projectname/api/login to /projectname/api/index.php?type=login
RewriteRule ^/?projectname/api/(.*)$ /projectname/api/index.php?type=$1 [R=301,QSA,L]
This will redirect or rewrite e.g /projectname/api/login to /projectname/api/index.php?type=login or /projectname/api/logout to /projectname/api/index.php?type=logout
Of core this is is also possible the other way around for rewrite:
RewriteEngine On
# Rewrite from e.g. /projectname/api/index.php?type=login to /projectname/api/login
RewriteCond %{QUERY_STRING} ^type=([^&]*)$
RewriteRule ^/?projectname/api/index.php$ /projectname/api/%1 [L]
And also for redirect
RewriteEngine On
# Redirect from e.g. /projectname/api/index.php?type=login to /projectname/api/login
RewriteCond %{QUERY_STRING} ^type=([^&]*)$
RewriteRule ^/?projectname/api/index.php$ /projectname/api/%1 [R=301,L]
But
If you have some HTML output and you want to change the output before it is served to the browser, .htaccess could not help you, you have to do it in your PHP application.
I'm using a .htaccess to create a clean URL but
when I checked my site
example.com/some-request it has duplicate content with
xyz.com/product_view.php?category_id=some-request
I'm finding hard how to fix this and this is my rewrite rule:
RewriteEngine on
RewriteBase /
RewriteRule ^([0-9a-zA-Z_-]+)$ product_view.php?category_id=$1 [NC,L]
What can be a better solution for this?
You need to externally redirect requests for
example.com/product_view.php?category_id=some-request
to xyz.com/some-request
before your internal rewrite.
Try the following:
RewriteEngine on
RewriteBase /
# Redirect direct requests for the real URL to your desired URL
RewriteCond %{THE_REQUEST} \?category_id=([^\ ]*)
RewriteRule ^product_view\.php /%1? [R=301,L]
# Existing rewrite to real URL
RewriteRule ^([0-9a-zA-Z_-]+)$ product_view.php?category_id=$1 [NC,L]
Bit of an aside... but it's only really duplicate if these URLs get indexed. If you have always linked to your "pretty" URL then the risk is relatively low. This can also be avoided by setting the appropriate rel="canonical" link element on your pages.
I have a website on a test server and I want to rewrite URL for this website because it is very long
I wish our visitors instead of entering this URL:
http://staging.company.fr/site2.it/s...oject2/public/
enter this URL:
www.monsite.com
I created a file. htaccess:
RewriteEngine On
RewriteRule ^/~(.+) http://www.monsite.com/~$1 [NC,L]
but does not work
While in .htaccess mod_rewrite doesn't match leading slash in a URL since it is applied per directory. Therefore following should work:
RewriteEngine On
RewriteRule ^(.+)$ http://www.monsite.com/$1 [L,R=301,NE]
This will redirect every URL in your existing domain other than home / to monsite.com
Reference: Apache mod_rewrite Introduction
I have a news (blog) site that returns urls in the following format when individual posts are selected:
website.net/sitenews.php?q=posts/view/postname/12
I am seeking to rewrite the url so that it reads:
website.net/sitenews.php/posts/view/postname/12
or any other way where the ?q= is removed for purpose of removing the ? so that the url can be accessed by facebook's like button as the facebook url linter does not parse query strings.
In the htdocs .htaccess file in the root directory I have tried the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} q=
RewriteRule (.*) website.net/sitenews.php/$1? [R=301]
This successfully removes the q=? however the rest of the string (posts/view/postname/12) is not returned and the url now looks as follows:
website.net/sitenews.php/sitenews.php
Does anyone have any suggestions to help me complete this url_rewrite?
Try this instead in your .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
%1 is capture group for query string q= (whatever comes after q=)
$1 is your REQUEST_URI
If you are using any CMS, like wordpress, or joomla or SE, then you have option to do that else you need to have an .htaccess file where you can write the code, refer this links
http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html
http://www.webmasterworld.com/forum92/2545.htm
http://www.google.com/#sclient=psy&hl=en&q=htaccess+change+the+url&aq=0p&aqi=p-p1g4&aql=&oq=htaccess+&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=c875dd2b8adea15a