htaccess & mod_rewrite: display dynamic php page with a folder - .htaccess

I would want an adress like this:
http://www.domain.com/some_page123/
to display the content of this page:
http://www.domain.com/script.php?var=some_page123
But i want the URL that appears in the browser to be www.domain.com/some_page123/
Is it possible to do that with htaccess even if the folder "some_page123" doesn't really exist ?
Also, i want to exclude the folder named "infos" from this rewriting. So if an user access the URL www.domain.com/infos/ then it will display the content of "infos" folder. I do NOT want to use script.php rewriting for the folder "infos"

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^infos/)^(.+?)/?$ /script.php?var=$1 [L,QSA,NC]

add .htacces in your root directory:
RewriteRule ^script.php - [L,NC]
RewriteRule ^infos/$ - [L,NC]
RewriteRule ^(.*)$ /script.php?var=$1
now I have tested it, it should be working.

Related

htaccess redirect to subfolder with url paramethers

I want to redirect every requests to my public/index.php file. i've tried
RewriteEngine On
RewriteRule ^(.*)$ public/index.php?url=$1
code that seems fine but its not working. my goal is change url form http://example.com/?url=user/profile/2 to http://example.com/user/profile/2.
my directory structure is
root
public
index.php
vendor
.htaccess
composer.json
To handle URLs like: http://example.com/user/profile/2 please try following Rules sets in your .htaccess file. Place your htaccess rules file into root directory. Please make sure to clear your browser cache before testing your URLs.
Options -MultiViews
RewriteEngine ON
RedirectBase /ApadanaCMS/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ public/index.php?url=$1/$2/$3 [L]
OR try following rules, please make sure either try above OR following rules at a time only.
Above will work for non-existing pages, to make it for any url(like you mentioned in your question) try:
Options -MultiViews
RewriteEngine ON
RedirectBase /ApadanaCMS/
RewriteCond %{REQUEST_FILENAME} !index\.php [NC]
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ public/index.php?url=$1/$2/$3 [L]
heres the solution:
uncoment (remove # ) mod_rewrite.so module in httpd.conf file in apache2 (in my case c:\Apache24\conf\httpd.conf) file. also change AllowOverride none to AllowOverride All in that file under DocumentRoot "${SRVROOT}/htdocs" section.
and this is .htaccess content
RewriteEngine on
RewriteRule ^(.*)$ public/index.php?url=$1 [QSA,L]
RewriteRule ^()$ public/index.php?url=$1 [QSA,L]
however if you need to get static files in your static directory add a new .htaccess file inside static directory with RewriteEngine off content

How to remove this part from URL using htaccess?

I have this URL:
http://www.register.abc.com/myapp/administrator/registration
Which is :
http://www.register.abc.com is my domain
myapp is my aplication folder in server
administrator/registration is my controller and function
What I'd like to do is to remove this part /myapp/administrator/registration from my URL. In default, when user only enter the http://www.register.abc.com/ it will be redirected to my home page. I'd like to set it so the user directly enter registration page not the home page. I have little to no knwledge about .htaccess. Any help is greatly appreciated.
Note: I am using wiredesignz codeigniter modular extensions hmvc
Edit:
my current .htaccess is like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /myapp
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
In your site root .htaccess (a level above myapp/ you may try this .htaccess code:
RewriteEngine On
RewriteRule ^$ myapp/administrator/registration [L,R=302]
If you want an internal rewrite (without changing URL) then try:
RewriteRule ^$ myapp/administrator/registration [L]

htaccess shows directory list rewrite mode not working

I'm currently working with those rewrite modes htacess...they works well
, Its possible to show http://domain/username and http://domain/post?id=123
PROBLEM
my Site directory is seen, as I type on the browser...
includes/? (http://localhot/includes/?)
account/? (http://localhot/account/?)
I dont want them to be seen....this is fixed by adding an index.php within that specific directory....but I cant handle to put an index page in all of them....
How do I fix this in htaccess?
HTACCESS
#post
RewriteRule ^post/?$ account.php?p=post [L,NC,QSA]
#user
RewriteRule ^([a-zA-Z0-9._-]+)/?$ account.php?p=profile&username=$1 [L,QSA]
You can add this directive:
Options -Indexes
on top of your .htaccess to disallow directory listing.
Options -Indexes
RewriteEngine On
#post
RewriteRule ^post/?$ account.php?p=post [L,NC,QSA]
# add trailing slash if missing from directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L]
#user
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ account.php?p=profile&username=$1 [L,QSA]

htaccess for SEO friendly urls

So for two days I can not understand why this won't work...:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/$ advice.php?advice=$1&id=$2
Obviously .htaccess is allowed since I am not able to directory open folders, but why doesn't the link work?
Try turning off multiviews so the server doesn't try and search for that as a file. In addition add the conditions so that it knows it's not looking for a real file or directory it's trying to serve and make the trailing slash optional.
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/?$ /advice.php?advice=$1&id=$2 [L]
Does your RewriteRule redirect to an existing file?
Try this, it works for me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /test2/test.php
http://example.com/test2/.htaccess shows the location of the .htaccess file
http://example.com/test2/test.php is a direct link
http://example.com/test2/testX4XX opens the same test.php file using rewrite

htaccess and rewriting urls

currently i have a /en/ folder that is empty except for a .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../index.php?language=en$1 [NC]
i use it to eliminate the need for
index.php?language=en
in all my URLs. I would like to modify the htaccess in a way that i no longer need the /en/ folder with nothing but the htaccess inside. ideally i would like an htaccess in my root folder that reads the url and if it is www.example.com/en/ to rewrite to www.example.com/index.php?language=en
This should work for you:
RewriteRule ^en/(.*)$ index.php?language=en$1 [NC]
Put the following code in .htaccess file in your root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)$ index.php?language=en$1 [L]
Here $1 will append rest of the url as well. The condition will also help if you request your files using direct url.

Resources