.htaccess URL needs to be created for sub folder view - php

I have enabled AllowOverride All in Apache configuration file.
This the URL users are typing in browser
http://hostname/system_name/dynamic_text/login
This needs to be working as below
http://hostname/system_name/index.php?id=dynamic_text
I have created rewrite rule as below. But its not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z])$/login index.php?login=$1 [NC,L]
Can you suggest better way to fulfill this requirement

Problem is with your regex pattern as [a-z] only matches a single lowercase letter. You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/login/?$ index.php?login=$1 [NC,L,QSA]

This is created as per answers and comments above and works for me as expected.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/login/?$ index.php?login=$1 [QSA,NC,L]

Related

URL ShortCut Redirects

This is my .htaccess command for making URL shortcuts:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .(.+)$
RewriteRule ^(.) ?open=encyclopedia&letter=$1&term=%1 [R,NC,L]
With the above works just fine:
example.com/Njumba ➛ example.com/?open=encyclopedia&letter=nj&term=njumba
Now, the problem is that it redirects merely one letter, but not two.
This is how I want it to be:
example.com/NJ ➛ example.com/?open=encyclopedia&letter=nj
Is this also achievable with .htaccess?
You can use this rule instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.{1,2}).*$ /?open=encyclopedia&letter=$1&term=$0 [R,NE,QSA,L]

Dynamic url change into seo friendly url

A very common problem, but couldn't fix the issue.
I want
http://website.de/page.php?module=helios
into
http://website.de/page/helios
I have tried lots of .htaccess code like this one, but still page sends to 404.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?module=$1
Please provide some suggestions.
Try this rule in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)/?$ page.php?module=$1 [L,NC,QSA]
Try...
RewriteRule ^page/([^/]*)/$ /page.php?module=$1 [L]
Use this use your base path
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.*)$ page.php?module=$1

Using RewriteEngine in PHP to read a file

recently I'm making a php application and I want some changes in my file.
So I use .htaccess and RewriteEngine and when I want to run my application it said 404.
Here is my code that I try and said 404 !!
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^access/(\d+)*$ show.php?address=$1&login=0
for example this my original address:
www.ex.com/show.php?address=5411496bc2b6c&login=0
And I want to change it to:
www.ex.com/access/5411496bc2b6c
Here is my question Am I doing something wrong in .htaccess ?
Try this rule:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^access/([^/]+)/?$ show.php?address=$1&login=0 [L,QSA,NC]
In your rule:
RewriteRule ^access/(\d+)*$ show.php?address=$1&login=0
Should be:
RewriteRule ^access/([a-z0-9]+)$ show.php?address=$1&login=0
(\d+) means digits and as it seems you need digits + lower alpha characters right?
Also SCRIPT_FILENAME will match executing script file name, so it will never be directory and you will also rewrite real existing directories, instead use REQUEST_URI or REQUEST_FILENAME
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
This works for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^access/([a-z0-9]+)$ show.php?address=$1&login=0 [L]

URL Rewriting confusion

I got a webpage that has the following URL:
localhost/Minecraft-User-Info/player/?user=matthijs110
Now I want it to localhost/Minecraft-User-Info/player/matthijs110
So without ?user=matthijs110
I tried different ways, but it doesn't seem to work. Mod_Rewrite is enabled.
What can I do to get this to work?
Give this a try.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Minecraft-User-Info/player/(.+)$ /Minecraft-User-Info/player/?user=$1 [L]
Edit:
Use this since it's in a sub directory.
RewriteEngine On
RewriteBase /Minecraft-User-Info/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^player/(.+)$ player/?user=$1 [L]
Updated to show my original answer and included the edit per users comment about where htaccess is located.
Try this instead. Minecraft-User-Info/player/username Redirects to Minecraft-User-Info/player/index.php?user=username
Tested on http://htaccess.madewithlove.be/ instead of setting up my own server for it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Minecraft-User-Info/player/(.+)$ /Minecraft-User-Info/player/index.php?user=$1 [L]
Try this
RewriteRule ^Minecraft-User-Info/player/([a-zA-Z0-9_-]+)\$ index.php?playerId=$1

Mod Rewrite and PHP

I am trying to redirect pages using mod_rewrite to the pages with some variables (for using them with PHP's $_GET).
To give an example with few lines:
When the user enters /c/stg it redirects to item_show.php?id=$1&f=1 (where f is page number).
When the user enters /c/stg/2 it redirects to the second page with show.php?id=$1&f=$2.
I think there are no errors or misuses of these in my file but, here's what I want:
I want user to enter /string directly to go item_show.php?id=$1&f=1 with $1 is our string of course...
But when I change my file by removing the /c/ part from RewriteRule it starts giving errors in all other directories and doesn't read any files (my.css) even though I have already defined a RewriteCond %{REQUEST_FILENAME} !-f...
Do you have any suggestions?
Or how can I made this system possible with any method?
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteRule ^c/([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Am I wrong if RewriteCond only are passed on to the first RewriteRule??
Try this instead:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Cheers,
RewriteCond directives do only belong to the first following RewriteRule directive. So in your case the two RewriteCond direcitves are only applied to the first RewriteRule directive and not to the others.
But you could use this abortion rule to quit the rewrite process if the request can be mapped to an existing file or directory:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
This would cause that following rules are only tested if the request cannot be mapped directly to an existing file or directory.

Categories