I had a website which url is http://www.testingmyweb.comli.com
In my website a had a folder TCWEB. My website is in this folder.
I want to map http://testingmyweb.comli.com/TCWEB/home.html to http://testingmyweb.comli.com/
What can I changes do in .htaccess file?
Based on your comment, your htaccess file looks like this:
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Redirect /TCWEB/home.html testingmyweb.comli.com
The rule that you have appends .html to requests that are for html files (but don't have the extension). The redirect that you have underneath that needs to be removed. In order for you to rewrite / to /TCWEB/home.html, that mod_alias directive will cause a loop. Replace it with these rules:
RewriteRule ^$ /TCWEB/home.html [L]
RewriteCond %{THE_REQUEST} /TCWEB/home.html
RewriteRule ^ / [L,R=301]
The first rule makes it so requests for / maps to /TCWEB/home.html, but the URL in the browser's address bar remains http://www.testingmyweb.comli.com. The second rule does what the Redirect does, except it checks that the request was actually made for /TCWEB/home.html, and not an internally rewritten URI. So when someone goes directly to /TCWEB/home.html, they get redirected to /, then the first rule gets applied.
Related
I am create a website and i have some problem in ht-access. I want to redirect my page but URL doesn't changed. Below are the code of htaccess for redirect
Note: i am working on local host Xampp server:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /online-web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home.php$ index.php$1 [r=301,nc]
RewriteRule ^about_us.php$ default.php$1 [r=301,nc]
</IfModule>
... this will redirect successfully but it will also change url. I want url remain same about_us.php
You just need to remove the r=301 (redirect) flag from the RewriteRule. This will then change it to an internal rewrite (as opposed to an external redirect) and the URL in the address bar will not change. For example:
RewriteRule ^about_us\.php$ default.php [NC,L]
However, you must make sure you clear your browser cache first. The 301 (permanent) redirect will have been cached by your browser.
You should also escape the dot in the RewriteRule pattern in order to match a literal dot and not any character. eg. ^about_us\.php$
I've also added the L (LAST) flag to the rule since you probably don't want any additional processing.
And I've removed the $1 backreference, since this is not being used.
I'm trying to learn how to use htaccess rewrite, but I just can't get the hang of it. What I'm trying to do is make the following urls:
mysite.com/
mysite.com/views/clients
mysite.com/views/projects
mysite.com/views/estimates
...look like this:
mysite.com/
mysite.com/clients
mysite.com/projects
mysite.com/estimates
It should be simple enough. But I just can't make it work.
Assuming the URLs in your app are already of the form /clients, not /views/clients, then try the following in the .htaccess file in your document root:
RewriteEngine On
# Remove "views" from all (typed) URLs with external redirect
RewriteCond %{THE_REQUEST} /views/([^\ ]*)
RewriteRule .* /%1 [R,L]
# Internally rewrite specific requests back to /views
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(clients|projects|estimates)/(.*) /views/$1/$2 [L]
Once you are satisfied this is working then change the R (temporary) redirect to R=301 (permanent).
This checks against THE_REQUEST before the redirect in order to prevent a redirect loop.
I'm trying to direct all traffic to the homepage only to a php script called go.php that gets a variable from the URL.
If someone visits domain.com/username go.php gets the username, looks up their information, saves the information to a session and then redirects to index.php and displays a modified version of the homepage (same domain) that has the retrieved information. Everything works except the mod rewrite part.
I tried the following and am not sure what I am doing wrong:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^/$ go.php?id=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My logic was that if a request is to index.php it should be allowed, to prevent looping.
If the request is to the homepage it will go to go.php?id=username and the that script will redirect to index.php and trigger the prior mod rewrite rule to prevent looping.
Otherwise, it will do the regular redirect to index.php if the directory or filename doesn't exist.
Any thoughts on how to fix this?
I think this is what you mean:
RewriteRule ^(.*)$ go.php?id=$1 [QSA]
Explanation:
I'm looking at the ^/$ in the regular expression in the go.php rewrite rule. I've tested that and it appears to be an impossible match in that situation. One might be wanting to capture requests for root. But the forward slash is not passed to this portion of the RewriteRule for root. so a call for root (only) is ^$. And there's also no capturing parenthesis to feed the $1 you have appended to go.php?id=$1.
This is my current .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/khp/handler.php
RewriteRule (.*) /khp/handler.php?url=$1 [L,QSA]
The desired result is for all urls, no matter whether or not they exist on the server, to be rewritten internally to /khp/handler.php?url=$1, where $1 is the original url.
What is currently happening is when I go to a url that is the name of an existing folder, it does this:
Requested url: example.com/khp
Redirects to: example.com/khp/?url=
Ideally, it would also redirect to remove all trailing slashes (ALL, whether an existing directory, the bare domain name example.com, etc). This was in a previous iteration of my htaccess file, but I removed it because it was creating an infinite redirect loop on example.com/khp
What am I doing wrong?
That happens because /khp is a real directory and mod_dir runs after mod_rewrite and adds a trailing slash in the rewritten URI.
To turn this behavior off use:
DirectorySlash Off
RewriteEngine On
RewriteBase /
# everything
RewriteCond %{REQUEST_URI} !=/khp/handler.php
RewriteRule (.*) /khp/handler.php?url=$1 [L,QSA]
# add a trailing slash to directories via a rule
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
I want my urls to be extensionless, so no .php extension, I also want there not to be an opportunity to access the URL with a trailing slash.
The following removes php extension and thens redirects to the extensionless url if you try to access it with .php
I started writing a rule to stop you accessing with a / and redirect, but it does not work, any help?
#this removes php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# stops you accessing url with.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]
# stops you accessing url with / **DOESNT WORK**
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [R=301,L]
You're looking at things backwards: the first rule you have doesn't "remove the php extension", it adds it to URLs that don't already have it (technically, any that don't contain a period).
I think you want something more like this:
RewriteEngine On
RewriteBase /
# Remove .php from any URLs that contain it, using an external 301 redirect
RewriteCond %{QUERY_STRING} !^no-redirect-loop(&|$)
RewriteRule ^(.*)\.php$ $1 [NS,R=301,L]
# Now add it back internally
RewriteCond %{QUERY_STRING} !^no-redirect-loop(&|$)
RewriteRule ^(.*)$ $1.php?no-redirect-loop [NS,QSA]
Edit: While debugging another similar answer, I realized that the previous solution I posted here wasn't going to work in an .htaccess file. I've edited the example code above to use a rather ugly kluge for breaking redirect loops instead. A side effect of the kluge is that all scripts will see an extra empty URL parameter named no-redirect-loop.