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]
Related
I'm currently trying to use "pretty" URLs instead of using filenames through rewriting in the .htaccess.
This is what I would like to have:
site.com/dir/report1 shows site.com/dir/report.php
site.com/dir/report1/ shows site.com/dir/report.php
I don't want a redirect but just the file to be loaded without leaving the pretty URL.
The first one works, but with the all calls to e.g. stylesheets now have "report1/" as base directory and thus can't be found.
Is there any way around this?
Here's my code:
RewriteEngine On
RewriteBase /dir/
RewriteRule ^report1/?$ report.php [NC,PT,L]
With help from #arkascha I've been able to solve this by redirecting all requests to "/report1/" to "/report1". This is my code:
RewriteEngine On
RewriteBase /dir/
RewriteRule ^report1/$ report1[R=301]
RewriteRule ^report1$ report.php [L]
I am looking to serve /myweb/index.php file as the default landing page when someone tries to access the domain using myweb.com. I tried using several combinations inside .htaccess but failed to achieve the result.
Another part of the requirement is that other URL redirection shouldn't have a problem due to this particular rewrite url, in other words rest of the url shouldn't look inside /myweb/ to serve images and whatnot.
Right now I have these inside my .htaccess
RewriteRule ^$ myweb/index.php [L] (This does not)
RewriteRule ^shops/(.*)$ /myweb/shops/$1 [L] (This works)
RewriteRule ^content/(.*)$ /myweb/content/$1 [L] (This works)
.htaccess file is placed at /var/lib/tomcat7-cms/webapps/ROOT
Anyhelp regarding this would be much appreciated.
I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.
I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.
I have a site that has another site inside of it in a sub folder. For example, www.domain.com and the sub folder would be www.domain.com/uk/. They both contain duplicate files, so even the .htaccess file is same. Now the problem is when I redirect users through .htaccess inside the uk subfolder to a file in the same directory it redirects them to the file in the parent site with the duplicate name.
This is the rule I have used in my .htaccess file:
RewriteRule ^category/[a-zA-Z0-9_-]+/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ final_final.php
I don't have much experience with mod_rewrite.
in the parent directory RewriteBase should be /
RewriteEngine On
RewriteBase /
in the subdirectory the RewriteBase should be /uk
RewriteEngine On
RewriteBase /uk
you can also specify concrete path in concrete redirect, it is not quite "clean" like Gustonez way but it may work too
in uk-htaccess redirect to uk base directory
RewriteRule ^category/(.*)$ /uk/$1 [L,R]
and in parent-htaccess redirect to base directory without /uk/
RewriteRule ^category/(.*)$ /$1 [L,R]
but redirect you have is weird i think
^category/[a-zA-Z0-9_-]+/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ final_final.php
do you really need all this parameters in url to redirect?
and when you redirect any of these parameters will be parsed on page
you just going to final_final.php page without sending parameters to this page
another thing is that you don't have [L] flag a the end of statement
you better tell exactly what you need because this redirect you have may be inadequate and may not work at all