Mod-rewrite.htaccess not working - php

Hi I previously posted a question on to Stack Over flow and was lucky enough to receive some assistance to my initial question which helped me better understand that what i was trying to achieve was possible.
The previous question is here: Visit php url shortening
I am building a system where users can share their profile on other websites so i am wondering is it possible to shorten the actual url which would provide a link to their profile which would be something like this, www.somedomain.com/users/profile.php?user=myusername to simply cut out the users folder and the profile page and so something like this: www.somedomain.com/myusername
the code I have is:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /(.*) /users/profile.php?user=$1
Sadly two things happen, firstly the code doesn't work and secondly when i add this code the css on the original long domain stops working.
Any suggestions would be appreciated

code doesn't work: of course it won't because it's incorrect. Really wondering how did you end up accepting other question without properly testing it. Anyway take this code and replace your existing .htaccess code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php/?$ - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users/profile.php?user=$1 [L,QSA,NC]

This is serverfault question.
Are your running apache?
Are apache mod_rewrite module enabled?
debian: a2enmod rewrite; /etc/init.d/apache2 restart
Are enabled override for this directory? In case using default vhost:
<Directory /var/www/>
#AllowOverride None
AllowOverride FileInfo
</Directory>
What are showing error.log?
tail /var/log/apache2/error.log
Try simple version:
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^profile.php profile.php [QSA,L]
RewriteRule ^(.*)$ profile.php?user=$1 [QSA,L]

Related

how to redirect to specific file by writing rule in .htaccess file?

I have developed a project without using any framework. I want to redirect a page to specific php file (example : movie/movie.php) when click on this
Click me
link. I have created .htaccess file and currently it look like this.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/movie/([0-9]+)/$ http://localhost/tthtml/movie/movie_review.php [L]
</IfModule>
I have enable mod_rewrite module in httpd.conf file. I am using wamp server in windows machine.
I dont no how to do this. I have referenced few sites and I created this above rule in my own. Kindly any one help me to do this and feel better if your answer have explonation as well. Thanks in advance.
Modify the rule as follows:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/([0-9]+)/(.*)$ http://localhost/tthtml/movie/movie_review.php [L]
</IfModule>
The rule that you placed was not working because it was comparing only the string "/movie/1/", while you actually want to match "/movie/1/Baashha-Suresh-Krishna-1995".
For this to work, I have modified the RewriteRule directive as ^/movie/([0-9]+)/(.*)$ to accommodate the remaining string "Baashha-Suresh-Krishna-1995" or any other value that the application may have.

Dynamic url in php without filename

I couldn't find exact answer to my question so I am posting this. Maybe it involves htaccess configuring.
What I would like is to have http://www.example.com/static_folder/dynamic_parameter
the above url should run the index.php in static_folder and I should be able to get that dynamic_parameter and output accordingly.
How to achieve this?
Thanks.
A littlebit of htaccess can do it pretty easily. As for example, save this rule in the root directory's htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/static_folder/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /static_folder/index.php [L]
</IfModule>
Then use $_SERVER['REQUEST_URI'] inside /static_folder/index.php to get the dynamic_parameter.
Is it what you are trying to achieve?
Try this in your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
then you can do:
sudo a2enmod rewrite
sudo service apache2 restart
Also check answers for this question: How to remove all .php extension from a dir and all of its subdirectories?
I believe the OP asked for index.php inside static_folder to get the variable, so, something like this, should do the trick:
RewriteRule ^static_folder/([^/]+)$ static_folder/index.php?variable=1$ [NC,L]

Rewrite Friendly php URL for multiple languages using .htaccess file

I have a couple websites that I built a CMS for that will save the languages in an XML file and depending on the page and language selector, it will show the correct data. I cuurently have 8 different languages for each of the sites and the urls are like:
http://leclosdamboise.com/index.php?lang=fr
http://leclosdamboise.com/rooms.php?lang=en
http://leclosdamboise.com/hotel.php?lang=de
I am trying to have it so that the urls are rewritten to look like this:
http://leclosdamboise.com/fr/index.php
http://leclosdamboise.com/en/rooms.php
http://leclosdamboise.com/de/hotel.php
I have spent hours combing through forums and not been able to find a solution that works. My .htaccess file currently looks like this:
SetEnv PHP_VER 5_TEST
SetEnv REGISTER_GLOBALS 0
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]+)$ index.php?lang=$1 [QSA,L]
This does not do anything and the urls are still the ugly step-daughter version. The links above are actual links to one of the sites. Once I sort this out I need to deply it to several sites, but been racking my head trying to sort this out. Can anyone tell me what I'm doing wrong?
This should work in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^.]+)\.php /$2.php?lang=$1 [L,NC,QSA]

.htaccess mod_rewrite for pretty urls not working

Trying to get www.example.com/test.php?archives=testing to www.example.com/archives/testing
According to godaddy they have mod_rewrite enabled by default
Here is my .htaccess file:
rewriteengine on
rewritecond %{HTTP_HOST} ^example.com$
rewriterule ^example\/(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
However this is not working, when i go to www.example.com/archives/test I get a 404, suggestions
I just left this in a comment but i might as well put it here so its seen easier. I wrote an answer for someone thats helped others out over time, and in the end this isn't exactly an answer to what your asking however its more of a stepping stone in the direction. The original question was asked how to work with short url strings and make them work in a fashion like your looking for, but rather copy and paste that answer here. Ill let you go there and read over it.
Its not to go without saying you will need to alter the rule a little for your specific needs but it will in the end serve its purpose for getting you where you want to be.
PHP dynamic DB page rewrite URL
You need some rewrite conditions to specify when this rule will be used. Without them, you will keep running the same rewrite rule indefinitely, giving you an error. Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
How about this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(.+)$ /test.php?archives=$1 [L]
</IfModule>
Without is being super clear what you are trying to get from your rewrites I suggest:
Options -MultiViews
ErrorDocument 404 default
RewriteBase /

Convert PHP Variable URL with mod_rewrite to clean URL

I need to convert a link that contains php variables into a clean link that can be picked up and indexed by Google. I've looked at ways to do this, and using mod_rewrite apparently is the best way to handle this.
Currently my URLs look like this:
http://mysite.com/jobs/view/?j=senior-model-validator-groep-risk-management
I'd like to end up looking like this
http://mysite.com/jobs/view/senior-model-validator-groep-risk-management
It's probably really easy but sadly I know nothing about .htaccess. I tried a few things but I end up with nothing remotely close :-)
Thanks in advance!
Edit: Here's my full .htaccess file based on the help so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You may use an .htaccess file at the root of you web site with these rules:
RewriteEngine on
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
Do not forget to filter/validate GET input in the script dealing with the data...

Categories