htaccess hide url ?code= and read it from php file - .htaccess

i want a change the .htaccess file to hide the url parameters .
I would like to accept urls like
http://www.demo.com/74874 , that means http://www.demo.com?code=74874

You didn't mention what operating system you are using and instruction can vary, plus you didn't say if your are using HTTPD and if mod_rewrite is already installed/enabled. Assuming mod_rewrite is enabled and AllowOverride FileInfo is set in the server config, you could use this in your .htaccess:
RewriteEngine on
RewriteRule ^/([0-9]+) ?code=$1
This will accept any number as the path and rewrite it with the query string. If you want to keep any existing query string parameters, you would need to add [QSA] to the end of the RewriteRule.

Related

htaccess Redirect or Rewrite URL - adding a segment to the URL without causing an infinite loop

I have a URL that I need to add a segment /#!/ to.
This isn't working because I get a loop.
Options +FollowSymLinks
RewriteEngine On
RedirectMatch "/constantsegment/(.*)" "/constantsegment/#!/$1"
For example, let's say my URL is http://example.com/constantsegment/changeablesegment. I need the site to redirect to http://example.com/constantsegment/#!/changeablesegment.
Notice that there is a URL segment(folder) called #! in between constantsegment and changeablesegment.
The problem I am running into is that a redirect loop is created when trying to append /#!/ to the /constantsegment/. How can I just added /#!/ to the end and then add all the other segment(s) after that.
Again
http://example.com/constantsegment/changeablesegment
Should redirect to
http://example.com/constantsegment/#!/changeablesegment
Another example (in this case products is the constant segment)
http://example.com/products/cars/blue
Should redirect to
http://example.com/products/#!/cars/blue
RedirectMatch "/constantsegment/(.*)" "/constantsegment/#!/$1"
Simply change .* to .+ (1 or more) to avoid matching the target URL and causing a redirect loop. Because the fragment identifier is not passed back to the server on the redirected request.
You can avoid repetition by capturing the first "constant" path segment as well. For example:
RedirectMatch "/(constantsegment)/(.+)" "/$1/#!/$2"
Note that the above mod_alias directive (ie. RedirectMatch) has nothing to do with mod_rewrite (ie. RewriteEngine On). And neither is Options FollowSymLinks required here. Unless you are using mod_rewrite elsewhere in your .htaccess file.
If you are already using mod_rewrite for other redirects then you should probably use mod_rewrite (ie. RewriteRule) instead (to avoid unexpected conflicts). For example:
Options FollowSymLinks
RewriteEngine On
RewriteRule ^(constantsegment)/(.+) /$1/#!/$1 [NE,R,L]
The NE flag is required to prevent the # (hash) being URL encoded and considered part of the URL-path.
Note that all these redirects are 302 (temporary).

Using htaccess to redirect to certain extension

I want to know how to use .htaccess to redirect a certain path to a certain extension. To be more clear, I want to redirect something like this:
http://www.example.com/api/some/page
To this:
http://www.example.com/some/page.json
I understand that I could just do this using the router that is supplied by CakePHP, however, how would this be done with a .htaccess file?
To handle this rewrite, you may use this rule just below RewriteEngine On:
RewriteEngine On
RewriteRule ^api/(?!.+\.json$)(.+)$ $1.json [L,NC]
(?!.+\.json$) is a negative lookahead that skips matching URIs that end with .json (to avoid a rewrite loop)
Pattern ^api/(?!.+\.json$)(.+)$ matches URIs that start with /api/ and captures part after /api in $1
$1.json in target adds .json at the end of matched part
Flags: L is for Last and NC is Ignore case

Why doesnt this htaccess rewrite work?

Okay so I am trying to make it so that if people go to /?char=USERNAME it would show the contents of /game/CharWidget.swf?login=USERNAME. This is my code so far:
RewriteEngine on
RewriteCond %{QUERY_STRING} char=(.*)
RewriteRule ^index.php?char=(.*) /game/CharWidget.swf?login=%1
This makes the url server side as /game/CharWidget.swf but doesn't carry the ?char=username and make it ?login=username so it wont show what I want it to show.
Edit; If it's easier doing /char/USERNAME to /game/CharWidget.swf?login=USERNAME i wouldnt mind doing that if someone could give me the code for it.
The query string is not visible to RewriteRules, so ^index.php?char=(.*) will never match. (Except that, since you haven't escaped . or ?, it will match e.g. indexZphchar=foo, which is probably not what you want.)
Also, if the user visits /?char=USERNAME, what the RewriteRule would normally see is just /; no index.php. Finally, if this is in an .htaccess file, you'll generally also need a RewriteBase directive.
Putting all those fixes together, something like this should work:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^char=(.*)$
RewriteRule ^/?(index\.php)?$ /game/CharWidget.swf?login=%1 [NS]
(The regexp ^/?(index\.php)?$ will match either an empty path or index.php, with or without a leading slash. That makes it a bit more complex than absolutely necessary, but also more robust. In particular, the /? lets it also work outside .htaccess files, where the leading slash will be present.)
Ps. The regexp ^char=(.*)$ will also allow URLs like /?char=foo&bar=baz to be rewritten to /game/CharWidget.swf?login=foo&bar=baz. If you don't want to allow such rewrites, replace it with e.g. ^char=([^&;]*)$.
Edit: Unfortunately, this isn't going to work for .swf files, because those execute on the client, and so won't see any changes to the query string made by server-side rewrites.
What you could do is make the rewrite external by replacing the [NS] flag with [NS,L,R=302]. However, this will also change the URL shown in the browser address bar, which may not be what you want. If so, another option would be to make the original request serve an HTML page on which you embed the .swf file.

htaccess remove index.php?page=

I currently have urls that look like: something.com/index.php?page=pagename
we would like to have it just be something.com/pagename
But still be able to access sub folders like something.com/admin/
Thanks in advance.
If you're using Apache, which I presume you are, you need to use the ReWrite Engine:
If you don't have one already, create or add to the .htaccess file stored in the root directory you're rewriting. So you if you want something.com/index.php?* to rewrite, then use put it in the folder where something.com is stored.
There, you need something like:
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?topic=$1 [QSA,L]
This regex takes the beginning of the input after "/", and uses that input as the variable $1.
Source: http://www.generateit.net/mod-rewrite/
You then change all your links to point to "/pagename"
You may also have to turn on the RewriteEngine module by uncommenting it in your httpd.conf file by finding the line like:
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
and deleting the leading #
More info: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

.htaccess 301 Redirect Appending Query's to End of URL

I am hoping someone can help with an unusual situation.
I have one main rewrite rule in place in my httpd.conf file which handles all of our dynamic content. The rule looks like this and works fine:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
The problem I have is that when I try to use .htaccess to create a simple 301 redirect, the query parameters are automatically appended to the end of the URL's so the final result looks like this:
http://www.example.com/category/page.html?&a=/category/subcategory/something/d/page/
Notice that the query string is appended to the URL when using .htaccess to create a 301 redirect.
I have solution for this on a case-by-case basis, but it's not practical to create a new rule each time I want to do a simple 301 redirect.
So, I am wondering if I can edit my "main rule" in any way so that when .htaccess is used to create redirects, the query parameters are not appended to the target URL.
Thanks in advance for any help you can provide.
If you have multiple simple redirects for which you want to suppress query string values you could put all the redirects in a RewriteMap (since you already have access to httpd.conf), and have one .htaccess rule that suppresses the query strings as below
place in htaccess
#if there is a match in the map
RewriteCond ${redirect_map:$1} !=""
RewriteRule ^(.*)$ ${redirect_map:$1}? [R,L]
place in httpd.conf
RewriteEngine On
RewriteMap redirect_map txt:/usr/local/apache/conf/redirect.map
contents of /usr/local/apache/conf/redirect.map
key followed by a space followed by target
directory/subdirectory1/subdirectory2/ example/category7/subdirectory/file.html
directory4/subdirectory2/subdirectory9/ example/category5/subdirectory4/file332.html
That's what your rule has defined it to do:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
It says to create a URL that will look like:
category/refine.cgi?&a=/foo/bar/
If you don't want that to happen, change your rule to be:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?

Resources