.htaccess redirect doesn't hide url

asked14 years ago
last updated14 years ago
viewed1.6k times
Up Vote1Down Vote

my .htaccess in the root folder includes the following lines :

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]

when I open the address http://example.com/bla.htm, my browser doesn't hide the GET values specified in the .htaccess, it redirects me to ?city=bla. eventhough I'm not using the [R] switch. This has always worked for me before (as I remember, haven't dealt with htaccess in a while). What's wrong here ?

11 Answers

Up Vote9Down Vote

When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).

Assuming both URLs are on the same server, you need to do something like

RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]

Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.

Up Vote9Down Vote
Grade: A

The code you provided is designed to rewrite the URL bla.htm to example.com/?city=bla and add a query parameter city with a value of bla to the rewritten URL.

However, the [NC] flag in the rewrite rule is causing the problem. The [NC] flag makes the rewrite rule case-insensitive, which means that the rewrite rule will match both bla.htm and Bla.htm.

This is causing the rewrite rule to redirect you to example.com/?city=Bla instead of example.com/?city=bla.

To fix this, you need to remove the [NC] flag from the rewrite rule.

Here's the corrected code:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [L]

Now, when you open the address http://example.com/bla.htm, your browser will redirect you to example.com/?city=bla.

Up Vote9Down Vote
Grade: A

It seems that there is a slight misunderstanding regarding how the RewriteRule directive works in Apache's .htaccess. The RewriteRule directive rewrites the URL from the client's perspective, but it doesn't actually modify the URL in the browser's address bar, unless you use the [R] or [R=301] flag.

In your case, you are expecting the URL in the browser's address bar to change to http://example.com/?city=bla, but that is not what's happening. Instead, the URL is being internally rewritten, so when you access http://example.com/bla.htm, the server processes it as if you had accessed http://example.com/?city=bla.

To clarify, the .htaccess rule you provided correctly rewrites the URL internally, and you can access the city query parameter in your PHP code using $_GET['city'], but it will not hide or modify the URL in the browser's address bar.

If you would like to redirect the user to a new URL and update the address bar with the new URL, you should use the [R] or [R=301] flag in your .htaccess rule.

Here's an example of how to redirect the user to a new URL with the [R] flag:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC,R]

This will redirect the user to http://example.com/?city=bla when they access http://example.com/bla.htm, and update the address bar with the new URL.

However, if you want to keep the URL in the browser's address bar unchanged (internal rewrite), your original rule is correct, and you can use $_GET['city'] in your PHP code to access the value of city.

Up Vote8Down Vote
Grade: B

The issue with this rewrite rule is that the RewriteCond is not defined. The RewriteRule needs a RewriteCond before the RewriteRule.

Here is the corrected code with the RewriteCond added:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/(.*)\.htm$
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]

The RewriteCond ensures that the rewrite rule only applies when the requested URI starts with / and ends with .htm. This ensures that the RewriteRule only targets URLs with the desired extension.

With this fix, the browser will now hide the GET parameters and display the page without the URL showing in the address bar.

Up Vote7Down Vote
Grade: B

Your current .htaccess rules will only redirect if the URL ends with .htm. However, it appears to not be working when you are trying to hide these GET parameters. This could have happened because of several reasons:

  • The mod_rewrite module may not enabled or functioning as expected on your server. Make sure that your .htaccess file is correctly placed in the root directory (not within any directories) and it contains at least one rule to avoid an "It works" response when trying to access directly via browser for testing purposes.

    • If you have disabled this module, re-enable it by adding RewriteEngine on to your .htaccess file
    • Use the command line or cPanel / WHM (your hosting provider's interface) and ensure that mod_rewrite is enabled there too.
  • There could be other rules present in your .htaccess that may conflict with this one. Make sure you don’t have any conflicting directives or other rules elsewhere which could be interfering. If so, comment out those lines temporarily to check if the problem still exists.

    • Also, test these rules at a lower level using RewriteLog and RewriteLogLevel directives at top of .htaccess for more information about your rewrite process.
  • Ensure you're not specifying a domain within your RewriteRule as so:

RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC,L]

If the rule is only targeting .htm files in root directory then ensure to remove domain part from it and should look like:

RewriteRule ^(.*)\.htm$ /?city=$1 [NC,L]

The [L] flag at the end of a RewriteRule tells Apache to stop processing if this rule is matched - in other words, it stops looking through any more rewrite rules (without it, all remaining rewrites would be processed even if they conflict).

  • Make sure that there are no further rules being applied later on in your .htaccess file. If so, move these rules above your current rule or use the [L] flag at end of rule as shown before.

  • Lastly check for any other errors with Apache's error log (or whatever log is relevant to you - depending upon your server setup and config). Look for anything related to this .htaccess file that might point towards issues preventing it from functioning correctly. You may find a more detailed rewrite logging on at the start of these logs by adding

RewriteLog /path/to/your/logfile
RewriteLogLevel 3

to your .htaccess, but note that this will slow down page response times as it needs to write out log entries for every URL rewritten. Ensure you delete the file or adjust log settings after finding any issues so you don't have an accumulating log of hundreds (or even thousands) lines from a day's worth of usage, for instance.

Up Vote6Down Vote
Grade: B

When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).

Assuming both URLs are on the same server, you need to do something like

RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]

Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.

Up Vote5Down Vote
Grade: C

The issue you're experiencing may be due to the fact that the RewriteRule directive in your .htaccess file is not hiding the original URL, but rather redirecting it. To hide the original URL in the address bar, you need to use the [L] flag along with the [R] flag or just the [R=301] flag which is for permanent redirections.

Try updating your .htaccess file as follows:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]

# Old rule that does not hide URLs but redirects instead
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC,R=301,L]

Replace index.php?url=$1 with the actual file or folder path you want to reach when accessing .html files via this new rule. Note that if your website uses a different index file or doesn't require an index file, you should adjust accordingly.

The first rule, with the [L] flag, will hide the URLs in the address bar, while still providing the functionality of redirection to the desired location.

However, this updated .htaccess file may cause issues if there are other rules or conditions on your website. If you continue to experience difficulties, it would be a good idea to consult a web developer or further investigate the issue in detail.

Up Vote5Down Vote
Grade: C

The issue might lie in your server configuration or settings. Let's investigate further:

  1. Check if your Apache is configured correctly to run this .htaccess file and rewrite the requested URL accordingly. It would also be beneficial to double-check the port that you've given for 'http://example.com'.
  2. If your Apache doesn't include these instructions in its configuration, you can manually add the following options:
Options +FollowSymlinks
RewriteEngine on

and then create the appropriate rewrite rules:

RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]

Where "^" and "$" are anchors indicating that this pattern is matched at the start and end of the string, respectively. The "?" indicates a dynamic part in the URL, which will be replaced with 'city'. 3. If your Apache doesn't support rewriting URLs, you can use a middleware such as mod_rewrite to add custom behavior to how requests are handled. This allows for more complex behaviors to be added at runtime and can help with situations where redirection is needed based on certain criteria. 4. Additionally, it's also possible that there are other issues at play, so consider using a browser like Chrome or Firefox in the future to confirm your experience.

Up Vote4Down Vote
Grade: C

You may be missing the [L] flag which is used to stop the rewriting engine from processing any more rules after this rule has been processed. The correct line would look like:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC,L]

Also, it is worth noting that you should always clear your browser cache before testing these kinds of changes in .htaccess to ensure you don't have any cached redirects still active.

Up Vote3Down Vote
Grade: C

The [NC] flag in the RewriteRule directive is used to perform a case-insensitive match on the pattern. It does not affect the visibility of the GET values in the redirected URL.

To hide the GET values in the redirected URL, you need to use the [L] flag in the RewriteRule directive. The [L] flag causes the rewrite engine to stop processing the rules after the current rule is applied. This prevents the default Apache behavior of appending the GET values to the redirected URL.

Here is the modified .htaccess code:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC,L]

With this modification, when you open the address http://example.com/bla.htm, your browser will be redirected to http://example.com/ without any visible GET values.

Up Vote2Down Vote
Grade: D

There is an issue with your .htaccess file. Specifically, you have specified [R=301] in your RewriteRule line. However, this option is not recognized by Apache, so it will not work. To fix the issue, you should remove the [R=301]] option from your RewriteRule line. This will allow Apache to recognize the option and use it in your RewriteRule line.