Mode_rewrite adding index.php to end of url - php

I want to apologize for not being a strong knowledge of the language, but I have a problem like that.
I made a redirect through mode_rewrite now url is "localhost/url/url2/" apache and it redirects to
"localhost/url/url2/index.php" how to remove from url "index.php?"

if you want to remove only index.php then put code like this in htaccess,
RewriteRule ^/$ http://localhost/url/url2/index.php
place this htaccess file where index.php is

You can use this rule:
RewriteRule ^url/url2/$ /url/url2/index.php [L]
Browser won't show index.php in the URL input.

Related

htaccess changes doesnt respnd the way as it should to clean URL

I have bunch of URLs
http://www.mydomain.com/aboutUs.php
http://www.mydomain.com/contactUs.php
and so on....
The Rewrite Rule I wrote in my .htaccess file is
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ contactUs.php
RewriteRule ^([a-zA-Z0-9]+)$ aboutUs.php
and so on...
Now when I use the clean URL, let say for example,
http://www.mydomain.com/aboutUs
It points to homepage always...i.e. http://www.mydomain.com/, BUT the URL remains http://www.mydomain.com/aboutUs
And similar behaviour for other URLs as well.
Also the images in the banner doesnt appear while using clean URL... As I have used a src attribute for images as <?php echo HTTP_PATH;?>images/header-logo.png
Where, HTTP_PATH = "http://" . $_SERVER["HTTP_HOST"])
Could someone guide me whats wrong with the .htaccess or is it anything else?
UPDATE as per #HAKRE Comment
I need the URL to look like http://www.mydomain.com/aboutUs and it should point to http://www.mydomain.com/aboutUs.php
Try and use a redirect; put a [R] after the rewrite statement, or [R,L] if you want apache to stop there. Or you can use [R=301] format eg to send a 301 permanent redirect code as well.
So a row looks like this:
RewriteRule ^([a-zA-Z0-9]+)$ contactUs.php [R]
hope this helps.

Simple RewriteRule with no variables

I want to change a single URL on mysite from sitename.com/test123.php to sitename.com/article-about-stuff. There are no variables to consider, just a one-off URL change. Is this possible with mod_rewrite and RewriteRule? I currently have:
RewriteEngine on
RewriteRule ^article-about-stuff$ /test123.php [L,QSA]
At the moment the user is redirected, but the url is changed to test123.php. I want to keep the url.
I would also like to be able to redirect any traffic to test123.php to /article-about-stuff
How can I simply write out the destination URL and link to it with the prettier URL?
add the following directive to your .htaccess (this will redirect request from test123 to article-about...)
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^test123\.php$ /article-about-stuff [NC,R=301,L]
Note: the first condition assure that no previous redirection is made (to prevent redirection loop)
The answer to your question is "yes, it's possible," but I'm not sure why you need mod_rewrite for such a simple redirect.
RedirectMatch ^test123\.php$ /test123.php
See When Not To Use Rewrite.

Unique url htaccess or other redirect needed

I need to somehow make one unique url redirect.
For instance:
http://www.mydomain.com/shop needs to redirect back to http://www.mydomain.com
BUT
http://www.mydomain.com/shop/tshirts etc still needs to function, I cant manage to get this to work. No matter what I attempt anything containing /shop/xxx redirects which I dont want it to.
Thanks in advance!
Try this. Write this in .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_URI} /shop
Rewriterule ^$ http://mydomain.com/test/ [L,R=301]
You need to enable MOD_REWRITE.
Place a .htaccess file in the root folder of your server and have it contain:
RewriteEngine On
RewriteRule ^(.*)shop/$ http://mydomain.com/ [R,L]
Hope it works for you!
Your redirection seems simple enough, so you can use redirect directive for this.
In your .htaccess file, add this redirect.
Redirect 301 /shop http://www.mydomain.com/
301: permanent
302: temp
303: seeother
410: gone
mod_alias needs to be enabled in config.

Remove in between folder structure from the url in a php website

I have a php website having following folder structure (basic structure).
project_name
app
controller
model
view
css
js
img
index.php
So when I view index.php in WAMP the url is http://localhost/project_name/
But when I go inside the site (eg. login.php which resides under view folder) url is like this. http://localhost/project_name/app/view/login.php
I found that using .htaccess we can change the urls. So I tried this (in .htaccess).
RewriteEngine on
RewriteBase /
Redirect 301 /project_name/app/view/login.php /project_name/login.php
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
Now url is http://localhost/project_name/login.php It is correct. But it seems php does not use the original link to grab the file (ie. from /project_name/app/view/login.php) but from here /project_name/login.php
So it throws 404 error.
What should I change? Please help me, i am just trying to hide /app/view/ part from the url so that user won't see my folder structure. I have read about various ways of doing that for about 9hrs today but still couldn't get anything working correctly.
Hope my question is clear enough. Any help is greatly appreciated!
URI's passed through rewrite rules in an htaccess file has the leading slash removed, so your rule:
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
won't ever match anything because of ^/. Also, since you are rewriting to a URI that matches a previous redirect, your browser will see a redirect loop and say that it's not redirecting properly. You'll need to match against the actual request and not the URI:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project_name/app/view/login\.php
RewriteRule ^ /project_name/login.php [L,R=301]
RewriteRule ^/?project_name/login.php$ /project_name/app/view/login.php [L]

php htaccess redirect

i have a site in php.
now i want to redirect my older site url to new url.
whenever user enter my old site url in address bar using browser url must be redirect to my new url.
How can i do this using .htaccess file.
Thanks in advance.
If you have a page by page mapping you can do it like this:
RewriteEngine on
RewriteRule index.php http://www.example.com/index.asp [R=301]
RewriteRule prods.html http://www.example.com/products.xhtml [R=301]
The first part ex, index.php is the local name index.asp is the remote name, [R=301] Means you are using a 301 as prescribed in RFC2616 meaning the file has moved permanently.
If all the files map perfectly in some way to another server you can do it like this:
RewriteEngine on
RewriteRule (.*) http://www.example.com/$1 [R=301]
Here you capture all requests and say that the file can now be found on example.com whatever it is called.
Best of luck.
or edit your index.php at your old site
<php
header('Location: http://newsite.com');
?>
Redirect http://oldsite.com http://newsite.com

Categories