htaccess detect and rewrite - .htaccess

I am having a big time issue in using htaccess but got a little more familiar with the help of this forum. but there is one issue where I cant seem to solve with the search function of this forum or perhaps my key words are an issue of its own. here goes the scenario where I hope you guys would enlighten me on:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^u=([0-9])$
RewriteRule ^([0-9])/([^/]*)$ /example/index.php?u=$1&m=$2 [QSA,NC,L]
the above modrewrite is suppose to help me to detect if the parameter for "u" exist and its in numerical form, it will rewrite to the above rules. If else it would use the below rewrite rules.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^!u=([0-9])$
RewriteRule ^([^/]*)$ /example/index.php?m=$1 [QSA,NC,L]
Well, as you can see, its not working very well. I really do appreciate any help given here. thanks in advance.

I just found a solution to my own question. Ironic as that is. Here you go:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ example/index.php?u=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ example/index.php?m=$1 [QSA,NC,L]
It's for those who wants to know. =)

Related

Rewrite multiple url's with htaccess

I'm struggling with this problem a few weeks now. In Google Search Console I get many crawl errors with the same problem: Google cannot find url's that don't even exist.
I've looked in the html-code, but the relative url's are all fine. And I'm using the /-base for all my internal links. I think the problem is my .htaccess file.
On my website nationsleaguevoetbal.nl I have two url's with different rewrites:
/nieuws/item
/wedstrijd/id/land
'land' isn't used and is only for looking nice. Now Google Search Console can't find for example:
/wedstrijd/id/nieuws/item
It combines the two url's where it shouldn't.
My .htaccess rewrite looks like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?pagina=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^nieuws/([^/]+)$ /index.php?pagina=nieuws&item=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wedstrijd/([^/]+)/([^/]+)$ /index.php pagina=wedstrijd&id=$1&landen=$2 [QSA,L]
I thought the QSA would solve the problem, but the errors are coming back. Can you help me please?
Have it this way:
RewriteEngine On
# skip all files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^nieuws/([^/]+)/?$ index.php?pagina=nieuws&item=$1 [QSA,L,NC]
RewriteRule ^wedstrijd/([^/]+)/([^/]+)/?$ index.php?pagina=wedstrijd&id=$1&landen=$2 [QSA,L,NC]
RewriteRule ^(.+?)/?$ index.php?pagina=$1 [QSA,L]

how htaccess redirect file not found contain special word to other file

I have links as sample: with "med_ " in the file name
http://www.xaluan.com/images/news/Image/2016/10/09/med_357fa10d94d29d.img.png
I need to redirect it too the small file "small_" if it not found in server, and then if small_ file not found.. it will redirec to noimage.gif exiting on server
but my following code not work.. please help
RewriteCond %{REQUEST_URI} images/news/(.*?)med_(.*?)\.(.*?)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ /images/news/$1small_$2\.$3 [R]
thanks very much
udate: thanks I got answer from Croises bellow .. but need some bit more.. in case of small_ image not found too, what i need for the htacess make redirect to /noimage.gif
You can use:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule images/news/(.*?)med_(.*?\.(?:jpg|gif|png))$ /images/news/$1small_$2 [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(?:jpg|gif|png)$ /images/no_images.gif [NC,R,L]

Change URL format with .htaccess

I've been trying to get a better URL format than the regular one. I have to deal with parameters, and I'd like to get from :
http://www.whatever.com/embed.php?site=site1&id=videoid
to :
http://www.whatever.com/embed/site/site1/id/videoid
I've been trying to get something like this using .htaccess, but I still don't understand how it really works.
This is what I have for now :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^embed/(.*)/(.*)$ /embed.php?site=$1&id=$2
The three first lines actually hide the .php extension, which is okay, but the next one doesn't work as I wish it would !
Any one as an idea ?
Thanks in advance guys !
Try it,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^embed/site/([\w-]+)/id/([\w-]+)$ embed.php?site=$1&id=$2 [QSA,L]

htaccess code for custom directory rewrite

right now i currently have this script that's working
.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1
and this does exactly what i want. it displays:
example.com/userid
instead of
example.com/profile.php?username=userid
but now im trying to implement another aspect like this. im trying to have:
example.com/c/1234
instead of
example.com/c/postpage.php?link=1234
the directory seems to be messing with my .htaccess code and i cant even get close to it working.
any advice is welcome.
Try to change the source of your .htaccess file with this code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /profile.php?username=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^c/([^/]+)$ /c/postpage.php?link=$1

Trouble with htaccess mod_rewrite and directories

I've read dozens of posts about similar problems, but I can't get this one figured out.
I have SEO friendly URL's on my site that look like this...
http://www.website.com/tequila/bottle-name.html
http://www.website.com/whiskey/bottle-name.html
http://www.website.com/vodka/bottle-name.html
...where bottle-name is the actual name of the bottle.
I am doing a mod_rewrite to a PHP handler to actually deliver the pages. Each handler is located in each directory (tequila, vodka, whiskey).
The first one (tequila) is working, but the other two are not and I can't figure out why.
Here is the mod_rewrite code I have right now...
#Rewrite seo urls
RewriteCond %{REQUEST_URI} !^/tequila/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=tequila [L]
RewriteCond %{REQUEST_URI} !^/whiskey/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=whiskey [L]
RewriteCond %{REQUEST_URI} !^/vodka/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=vodka [L]
What appears to be happening is the whiskey and vodka pages are getting caught and processed by the tequila mod_rewrite (guessing because it is first in the list here).
So, http://www.website.com/vodka/vodka-bottle.html is actually getting rewritten as http://www.website.com/tequila/index.php?liquor=vodka-bottle.html&liquor-type=tequila.
It seems like I haven't setup the RewriteCond %{REQUEST_URI} !^/tequila/ correctly?
Any help or pointers anyone can provide are greatly appreciated!
Why are you negating the ^/tequila/? The ! will cause a match on all other liquor types, rather than the one you list.

Resources