On my site I have multiple URLs like this:
Main Page:
mysite.com
mysite.com/?content=about
mysite.com/?content=posts&page=2
Subfolders:
mysite.com/subsite/
mysite.com/subsite/?content=about
mysite.com/subsite2/?content=posts&page=2
I'd like to make clean these up to be:
mysite.com/about/
mysite.com/posts/2
mysite.com/subsite/about/
mysite.com/subsite/posts/2
Now, I've been able to use mod_rewrite for one variable, and some other simple things, but I'm not exactly sure how to accomplish this. When I use:
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /?content=$1&page=$2 [L]
It recognizes the sections of the URL as variables, but it also sees the subsite as a variable, and attempts to plug in 'subsite' for 'content'.
Would anyone be able to point me in the right direction?
You could tweak you existing rule to allow for an OPTIONAL subsite/ or subsite2/ prefix e.g.
RewriteRule ^(subsite/|subsite2/)?([a-z]+)/([a-z0-9-]+)/$ /$1?content=$2&page=$3 [L]
Or just add a rule to handle the subsites before the existing rule e.g.
RewriteRule ^(subsite/|subsite2/)([a-z]+)/([a-z0-9-]+)/$ /$1?content=$2&page=$3 [L]
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /?content=$1&page=$2 [L]
I ended up getting this to work by putting another .htaccess file in the subsite folder.
The root .htaccess has:
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/?$ ?a=b&content=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ ?a=b&content=$1&page=$2 [L]
In the subsite .htaccess I have: (same as root .htaccess)
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/?$ ?a=b&content=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ ?a=b&content=$1&page=$2 [L]
If you do not need to alter the subdirectory's url, add only RewriteEngine On to the subsite's .htaccess. This basically overrides the root .htaccess rewrite rules, and loads the page from the subfolder.
Related
I have a htaccess code that i want to be changed.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ proxy.php?url=$1 [L,QSA]
It works like a charm if i place proxy.php and .htaccess file in main website and visit pages through www.domain.com/
I want to change this RewriteRule in a way that if I place the proxy.php and htaccess in subfolder named "folder" then new results show in www.domain.com/folder/
So mainly purpose is to change results path from www.domain.com to www.domain.com/folder/
You can place your code without any changes in /folder/.htaccess with proxy.php.
Or you can use:
RewriteEngine on
RewriteBase /
RewriteRule ^folder/(.+)$ folder/proxy.php?url=$1 [L,QSA]
I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]
how can I show folder name instead of file name?
For example,
www.example.com/home/index.php -> www.example.com/home
I know when I browse www.example.com/home I will get the result but that is not what I want because it will add a / behind the folder name (e.g www.example.com/home/).
What I want is without the / behind the folder and when user browse www.example.com/home/index.php the page will redirect the user to page not found. The purpose I do this is to hide the language I used and make the link more readable and memorable.
I found something like rewrite the rules in .htaccess file but I am new in php so I don't how to make it. Anyone can give me suggestion or provides some tutorial about this.
Thanks.
To remove the slash, you must first disable DirectorySlash
DirectorySlash Off
DirectoryIndex disabled
Direct access to PHP files can be answered with a R=404 status code
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]
And then, you can rewrite requests pointing to a directory and containing an index.php
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^.*$ /$0/index.php [L]
This RewriteCond looks, if the requested URL is a directory and if there is an index.php in this directory. If this is the case, then the index.php is executed.
Putting all together
DirectorySlash Off
DirectoryIndex disabled
RewriteEngine on
# prevent direct access to PHP files
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]
# rewrite requests for a directory to index.php
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^.*$ /$0/index.php [L]
Htaccess doesn't have anything common with PHP. Htaccess is Apache's configuration file, so you need to play with htaccess to achieve that what you want. On the other hand your solution will be really dirty. Learn about MVC and FrontController to make your structure cleaner.
I need help with my mod_rewrite for a site im currently working on.
Let's say I have this site http://example.com
And I want to be able to make any value after the / to route to page.php like below
http://example.com/value1
http://example.com/value2
to point to
http://example.com/page.php?id=value1
http://example.com/page.php?id=value2
,respectively.
But, not route to that page when im pointing to "admin"
http://example.com/admin/
I've tried
RewriteEngine on
RewriteCond $1 !^(admin)
RewriteRule ^(.*)$ /page.php?id=$1 [L]
But it isn't working. Any thoughts?
$1 is not available at the time of the Condition. I believe what you are looking for is close to:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^admin/.*
RewriteRule ^(.*)$ page.php?id=$1 [L]
this rule causes everything not starting with admin/ to go to /page.php. I don't believe the %{param} is optional. Using RewriteBase / means you do not to have prepend / on /admin and /page.php; it may actually fault if you use /page.php instead of page.php
If you have means of accessing the server values, then the final rule can be:
RewriteRule . page.php [L]
You can find the called url in the REQUEST_URI
This of any help?
.htaccess mod_rewrite - how to exclude directory from rewrite rule
I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]