I goit this htaccess:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&title=$2 [L]
So I can rewrite example.com/123/hey to example.com/?p=123&title=hey
But now I can't call files like example.com/pics/jo.jpg directly any more
Is there any way to call the files directly and keep the rewrite rules by modifying the htaccess or something?
The reason why you can't call your /pics/jo.jpg is because your your pattern ^([^/]*)/([^/]*)$ matches all requests of this form /foo/bar , so when /foo/bar.jpg is requested your rule rewrites it to /?p=foo&title=bar.jpg . To solve this ,you need to exclude your existent files and dirs from the rule using RewriteConds
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&title=$2 [L]
Related
I havve been using an htaccess file to create rewrites so as not to not have to include .php/.html filenames in my urls.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-page.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example-page.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /front /front_page.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /redirect /redirect-page.php
However as my code is currently written the redirects are not working and all attempts at loading the pages with anything else besides the filenames is giving me a 404 error. How might i fix my code to get the redirecting to work?
Write the directives like this instead:
RewriteEngine on
RewriteRule ^front$ /front_page.php [L]
RewriteRule ^redirect$ /redirect-page.php [L]
There doesn't seem to be a need for all your additional conditions? I assume you only have the one domain example-page.com and you don't have a file called /front etc.
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash. You should also include start/end-of-string anchors on the regex so that you only match front exactly and not match it anywhere in the URL-path.
I have some folders in the root dir with an .htaccess file inside to deny for any access(deny from all). And my htaccess in root is:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
It's working fine except when I get the folders name in url. I want to ignore folders name and just redirect any url to index.php?url.
Could anyone help?
The !-d, !-f, !-l conditions mean only apply the rewrite rule if the access url does not resolve to an existing file, directory or link. So if the directory exists, it won't apply the rule. You need to remove those. You then need to prevent the recursive rewrite of index.php to itself like so :
RewriteRule ^index.php$ - [L]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
You probably want to also change .+ in the last rule to .* to also match the empty url.
Indeed it seems the inner .htaccess files are parsed first, before the RewriteEngine one. So you will need to remove those .htaccess and rely on the rewrite rule.
I have a rewrite rule:
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]
that rewrites example.com/index.php?page=something to example.com/something.
This rewrite rule doesn't let me into example.com/(some-file-here), for example:
example.com/favicon.ico
example.com/robots.txt
To show up the favicon, I simply put it into /images/favicon.ico and stuff like that, but I prefer a correct configured .htaccess and put favicon.ico and robots.txt into root dir.
What changes do I have to make in this rule so I can access example.com/(some-file-here)?
You need to exclude your real files and folders from the rule, try :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
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.
there's been similar posts about this but I can't quite seem to find what I need.
I want my .htacess to rewrite "up one level".
The Url would be somethign like
http://www.site.com/variable_dir/
or
http://www.site.com/variable_dir/sub_dir
I need that to basically rewrite the request to
http://www.site.com/
or
http://www.site.com/sub_dir
I DO want the URL to still show the original
http://www.site.com/variable_dir/
or
http://www.site.com/variable_dir/sub_dir
I currently have
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
This redirects to where I want, but this changes the URL to
http://www.site.com/
or
http://www.site.com/sub_dir
which I don't want.
I know it's simple but I just can't seem to get there.
The rule below woule rewrite http://www.site.com/variable_dir/ to http://www.site.com/ and http://www.site.com/variable_dir/sub_dir to http://www.site.com/sub_dir
RewriteEngine on
RewriteBase /
#for a request to /variable_dir
RewriteCond %{REQUEST_URI} ^/variable_dir/(.+)$
#rewrite it to directory without variable_dir
RewriteRule . /%1 [L]
Edit:
If the directory is not literally variable_dir, the rule above will not work. However, if you have a short list of directories, you could enumerate them as below.
RewriteEngine on
RewriteBase /
#only apply if this directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
#for any direcory enumerated here
RewriteCond %{REQUEST_URI} ^/(variable_dir|dir2|dir3|etc)/(.+)$
#rewrite it to directory without variable_dir
RewriteRule . /%2 [L]
If not, then ideally the directories would all have something in common so you could limit what the rule affects. If you want a completely variable dir, nothing in common, I don't recommend it, but you can try
RewriteEngine on
RewriteBase /
#only apply if this directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
#skip any top level directory
RewriteCond %{REQUEST_URI} ^/[^/]+/(.+)$
#rewrite it to directory without variable_dir
RewriteRule . /%1 [L]
Edit:
Finally if the trailing slash is optional, as in the example in your comment, change the RewriteCond and rule above to be
#skip any top level directory, optional trailing slash
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))?$
#rewrite it to directory without variable_dir
RewriteRule . /%2 [L]