Problems with url rewrite of query string - .htaccess

I'm trying to rewrite urls like http://www.url.com/blog/?p=123 to http://www.url.com/#blog/123. I've read up on things and found that you can only parse the query string in the RewriteCond so I tried something like:
RewriteCond %{QUERY_STRING} ^p=([0-9]*)$
RewriteRule ^.*$ /#blog/%0 [NE,R]
When I try this the urls end up being rewritten to:
http://www.url.com/#blog/p=213?p=213
Any ideas how to do this properly? Also, is there a way to add an additional RewriteCond that checks that the REQUEST_URI contains blog?

You can do this by removing the query string entirely:
RewriteCond %{QUERY_STRING} ^p=([0-9]*)$
RewriteRule ^.*$ /#blog/%1? [NE,R]
This should give you:
http://www.url.com/#blog/213
If you want to check if the URL contains the term "blog" then simply check:
RewriteCond %{REQUEST_URI} .*/blog/.*
It is important to note that you will not be able to check for "blog" in links like http://www.url.com/#blog because, as noted by Patrick, everything after the # is not sent to the server.
See the Apache wiki on mod_rewrite for more information.

That may not work because browsers do not send the fragment of the url after the hash (#) with the request, so any request for http://www.url.com/#blog/p=213?p=213 will be a request for http://www.url.com/
The hash fragment is supposed to be used for anchor tags on pages, and is never sent to the server.

Try this rule in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule ^blog/?$ /#blog/%1? [NC,NE,L,R]
With above a URL of http://localhost/blog/?p=1234 will become http://localhost/#blog/1234

Related

mod_rewrite and redirect causing loop

I have problem when I try to redirect and rewrite together.
I have site example.com/show_table.php?table=12 (max 99 tables). I wanted nice links, so I got this .htacces rw rule:
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
Now are links something like example.com/table/12 - it's definitely OK. But I want all old links redirect to new format. So I use Redirect 301, I added to .htaccess this code:
RewriteCond %{REQUEST_URI} show_table.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
But when I visit example.com/show_table.php?table=12, I receive just redir-loop. I don't understant - the first is rewrite, the second is redirection, there ain't no two redirections. Do You see any error?
Thanks!
Instead of checking REQUEST_URI in the condition, you need to be checking in THE_REQUEST (which contains the full original HTTP request, like GET /show_table.php HTTP/1.1). When Apache performs the rewrite, it changes REQUEST_URI, so to the rewritten value, and that sends you into a loop.
# Match show_table.php in the input request
RewriteCond %{THE_REQUEST} /show_table\.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
# Do a full redirection to the new URL
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
# Then apply the internal rewrite as you already have working
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
You could get more specific in the %{THE_REQUEST} condition, but it should be sufficient and not harmful to use show_table\.php as the expression.
You'll want to read over the notes on THE_REQUEST over at Apache's RewriteCond documentation.
Note: Technically, you can capture the query string in the same RewriteCond and reduce it to just one condition. This is a little shorter:
# THE_REQUEST will include the query string so you can get it here.
RewriteCond %{THE_REQUEST} /show_table\.php\?table=([0-9]{1,2})
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]

.htaccess | no redirect if there is an specific parameter

im doing a cms at the moment
now im struggeling with the ajax implementation
i have everything running except a mod_rewrite problem..
RewriteEngine On
RewriteRule \.html index.php [L]
this redirects nothing except html files to index.php
i need a second rule witch checks the REQUEST_URI for a parameter to prevent the full site gets loaded by ajax.
i dont think this is understandable so i just post what i want to achieve^^
RewriteEngine On
RewriteCond %{REQUEST_URI} !(?rewrite=no$)
RewriteRule \.html index.php [L]
i want nothing redirected except html files and also no redirect on url's with "(.html)?rewrite=no" at the end
hope someone can help me since rewrites and regexp are not my stongest stuff
thanks in advance
From the Apache docs:
REQUEST_URI
The path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as as its own variable named QUERY_STRING.
So you are actually looking to match on %{QUERY_STRING} rather than %{REQUEST_URI}. Don't include the ? on the query string when matching its condition:
RewriteEngine On
# Match the absence of rewrite=no in the query string
RewriteCond %{QUERY_STRING} !rewrite=no [NC]
# Then rewrite .html into index.php
RewriteRule \.html index.php [L]

URL Rewrite - Query String

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

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

htaccess mod_rewrite with dynamic parameters

I am new to mod_rewrite. I am trying to forward a URL to another one, but I cannot get it to work.
Say I want to forward this URL:
/cansas.php?m=2&id=2-0-0-0&sid=cansas to
/cansas-is-good-for-you and let the header respond with a 301, or just update the URL through [R].
I have this in my .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^cansas.php?m=2&id=2-0-0-0&sid=cansas$ cansas-is-good-for-you [NC,R=301]
I figured I could just do a simple forwarding, but somewhere along the way it stops working. If I cut out the ?m=2&id= etc, it forwards just the cansas part to the new part so it looks like this: cansas-is-good-for-you?m=2&id=2-0-0-0&sid=cansas.
How can I forward it when I have several dynamic parameters in the URL string? Example on pages I need to forward:
/cansas.php?m=2&id=2-0-0-0&sid=cansas
/cansas.php?m=2&id=2-1-0-0&sid=cansas
/cansas.php?m=2&id=2-2-0-0&sid=cansas
Any help would be greatly appreciated :)
Maybe it isn't possible to do it this way? The way I have set it up at the moment is that I want to use new URLs called /cansas-is-good-for-you which reads from the source /cansas.php?m=2&id=2-0-0-0&sid=cansas, but the URL shown in the browser should be: /cansas-is-good-for-you. I need to forward that old cansas.php? URL to the new URL :)
You need to check the query of a URL with the RewriteCond directive as the RewriteRule directive only handles the URL path:
RewriteCond %{QUERY_STRING} ^m=2&id=2-0-0-0&sid=cansas$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
If you want to check for just one parameter, use this:
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
And to do this just for initial requests, you need to check the request line:
RewriteCond %{THE_REQUEST} ^GET\ /cansas\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

Resources