I have a directory in my site with a bunch of temporary PHP generated PDF files.
mysite.com/crm/pdfs/
I have an .htaccess file located in the pdfs/ directory with the intention of redirecting all requests within that folder to the index.php page within that folder and include the original request in the query string. For example,
When a browser is pointed to:
mysite.com/crm/pdfs/somepdf.pdf
it should be redirected to
mysite.com/crm/pdfs/index.php?p=somepdf.pdf
Here's my current try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
The problem with this is, when the user requests:
mysite.com/crm/pdfs/somepdf.pdf
They are instead redirected to
mysite.com/index.php
and the original request is not appended.
How can I achieve the desired result in my .htaccess that is located in the /pdfs directory?
Use this rule in your /pdfs/.htaccess:
RewriteEngine On
RewriteBase /crm/pdfs/
RewriteRule ^(.+?\.pdf)$ index.php?p=$1 [L,QSA,NC]
Related
I have a project with the name of social app which contains has a structure like this:
includes
_login.php
_profile.php
pages
login.php
profile.php
currently if I want to vists any page like profile.php I have to visit localhost/social-app/pages/login.php but I want to modify it and change the url to localhost/social-app/login.php. basically I want to get rid of the pages and make my URL a bit cleaner
In your htaccess in the root , put the following :
RewriteEngine on
RewriteRule ^social-app/login\.php$ /social-app/pages/login.php [L]
Now instead of going to long URL you can just type /social-app/login.php to access the file in pages folder.
EDIT :
To remove the directory name for all files , you can use the following :
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/social-app/pages/$1.php -f
RewriteRule ^social-app/([^.]+)\.php$ /social-app/pages/$1.php [L]
If the rule above fails , then use this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^social-app/(.+)\.php$ /social-app/pages/$1.php [L]
This will internally map a request for /social-app/filename.php to /social-app/pages/filename.php .
If everything (ie. images, CSS, JS and PHP pages) need to be rewritten to the /pages subdirectory then you can do something like the following in the /social-app/.htaccess file using mod_rewrite:
RewriteEngine On
RewriteRule !^pages/ pages%{REQUEST_URI} [L]
This unconditionally rewrites everything that is not already prefixed with pages/ to the pages subdirectory. eg. /social-app/login.php is internally rewritten to /social-app/pages/login.php.
If you have static resources in other locations that should not be rewritten then include a filesystem check to prevent requests that already map to existing files from being rewritten. For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^pages/ pages%{REQUEST_URI} [L]
If you are changing an existing URL structure then you'll need to redirect the old URLs that are indexed by search engines and perhaps linked to from third parties. For example, the following "redirect" would need to go before the above rewrite:
RewriteBase /social-app
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^page/(.*) $1 [R=301,L]
I have web project where index file is on deep folder.
Lets say my project index is on localhost/xxx/a/b/c/index.php, but I want to hide /a/b/c path become localhost/index.php.
How to code .htaccess and where I should put it?
Thank you.
Try the following in public_html/.htaccess
RewriteEngine on
#if the request is not for and existent file
RewriteCond %{REQUEST_FILENAME} !-f
#and the request is not for and existent directory
RewriteCond %{REQUEST_FILENAME} !-d
#then rewrite it to /a/b/c/request
RewriteRule ^(?:xxx/)?(.*)$ /xxx/a/b/c/$1 [NC,L]
This will internally redirect
example.com/file.php
to
example.com/a/b/c/file.php
I have a problem with Htacces file , the thing is I am trying to do a Profile URL
ex : www.mysite.com/profilename --> this link will take me to the user profile and its working because of
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?u=$1
no the thing is whenever I try to access any other directory for example www.mysite.com/login it changes the url to www.mysite.com/login/u?=login
Depending on if login is an actual file or directory rather than another script needing another htaccess rule.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
This checks first to see if the directory or file exists before executing your RewriteRule directive.
I am trying to forward all requests for other index.php files to the root index.php file with the path encoded as a GET variable. Here are some examples of what I mean:
http://my-site.com/this/is/the/path/index.php would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php
http://my-site.com/this/is/the/path/ would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2F
http://my-site.com/this/is/the/path/index.php?v=var would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php%3Fv%3Dvar
How can this be done with the .htaccess file?
EDIT
To be clear, here is what I am hoping to achieve:
If a path is to a file called "index.php" it redirects as long is it is not the root index file. Note, said index.php file could have GET variables that should be included.
If a path ends in a slash, in should redirect as well as long is it is not the root. Again, note said path could have GET variables that should be included.
Otherwise, it should not redirect any other paths.
In summary, only paths to an index.php file (that are not the root) or that end in a slash should be redirected.
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]
You can use this .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?path=$1 [L]
</IfModule>
I suggest learning a little about regular expressions in Apache's mod_rewrite.
RewriteEngine On
RewriteRule ^(.*/index.php)$ index.php?path=$1
Note this will only rewrite URL's that end in index.php and leave all other URL's alone, as that's the behavior you specified. If you want to rewrite all URL's to the index:
RewriteEngine On
RewriteRule ^(.*)$ index.php?path=$1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
can any body explain me how above htaccess rule works
e.g. if I have URL http://mydomain/edituser
so what php script will match with given URL
earlier I write different rules for each given URL
but in above case how I know that witch php script get run
please help me
That rewrite rule matches any request that does not match an existing file, and routes the request to to index.php using PATH_INFO
Translation of the above code is like that:
RewriteEngine On: Activate the RewriteEngine if not already activated
RewriteCond %{REQUEST_FILENAME} !-f: If the requested file name is not a regular file
RewriteCond %{REQUEST_FILENAME} !-d: If the requested file name is not a regular directory
RewriteCond $1 !^(index\.php|images|robots\.txt): If the request is not the index.php, the images or robots.txt file
RewriteRule ^(.*)$ /index.php/$1 [L]: Send the request to index.php and stop ([L])
This looks as a part from WordPress. If the file doesn't exists (-f) and a directory also not exists (-d) and the request is not for index.php or images or robots.txt when call index.php with the path as a parameter.