Wordpress: Using PHP or .htaccess to change name - php

I am aiming for clean URL's in my WP site, currently I have the following example:
Example:
PluginName: = gft_galleries
pageTitle: = gallery-one
http://domain.co.nz/pluginName/pageTitle/
Is there away within Wordpress or .htaccess to create the following but keep the correct structure underneath?
Example:
http://domain.co.nz/galleries/pageTitle
.htAccess
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)(.*)$ [NC]
RewriteRule ^(.*)$ http://%2/$1 [L,R=301]
RewriteRule ^gft_gallery/(.*)$ /gallery/$1 [L]
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]

I think what you're looking for is this.
Add it to your .htaccess, if its wordpress though, your
rewrite engine will probably already be on, so omit that
line
#modified ;)
RewriteEngine On
RewriteRule ^gft_gallery/(.*)$ /gallery/$1 [L]
I guess another simple way to do this is with symlinks, by typing
$ ln -s gft_gallery/ gallery
into your terminal, or via a script
<?php symlink('gft_gallery','gallery'); ?>
remember if you use the PHP method above to add Options FollowSymLinks in
either your apache.conf or htacces

Related

Apache htaccess issue on modRewrite

I got an .htaccess file must to rewrite URLs for SEO friendly website,
So when i click on eg. an article ( eg www.example.com/article/this-is-an-artcle/ ) must to redirect me to index.php?article=this-is-an-article.
Same thing I must to do with the category page and admin page, so for completeness is something like this
www.ex.com/index.php?article=example-article -> www.ex.com/article/example-article
www.ex.com/index.php?category=example -> www.ex.com/category/example
www.ex.com/index.php?page=admin -> www.ex.com/adm
Instead of this I must to hide the index.php file from the URL.
I'm doing this, but produce a 404 error
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^article/([\w-]+)/?$ index.php?article=$1 [QSA,L,NC]
RewriteRule ^category/([\w-]+)/?$ index.php?category=$1 [QSA,L,NC]
RewriteRule ^adm/?$ index.php?page=admin [QSA,L,NC]
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Unfortunately If I open the apache.err.log can't see errors relating to the RewriteEngine, so i've tried to use LogLevel alert rewrite:trace6 in my .htaccess file, but didn't produce log, also tried to put a rewrite.log file in site folder, and make 777 permissions, no change, file still blank.
EDIT: Since this file work in another server ( cloud9 IDE ) and here not ( AWS Ubuntu 14.04 ) I'm assuming that I miss some configuration. How I can enable the LOG to see what's happen?
andreaem, try this:
RewriteEngine On
RewriteRule ^article/([a-zA-Z-_]+)+$ index.php?article=$1 [L]
RewriteRule ^category/([a-zA-Z-_]+)+$ index.php?category=$1 [L]
RewriteRule ^adm$ index.php?page=admin [L]
Run it through a syntax checker perhaps
E.g. http://www.htaccesscheck.com/
You are probably getting 404 error status becouse of the relative rewrite targets, add a slash before your traget paths ie :( /index.php )
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^article/([\w-]+)/?$ /index.php?article=$1 [QSA,L,NC]

How can I mask a "get" URL and make it look look like a file?

I would like to first apologize for my choice of words. I haven't been in the web dev business to properly word the title, so there's that.
I'm using Apache Server and I have a .htaccess file in the root folder of my project. It specifies a 404 page and also a rewrite condition for removing the .php out of a file in the URL:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
</IfModule>
I have a users section on my page "social.php" and retrieve them using a GET variable in my code.
Example URL:
www.mysite.com/users/social.php?username=someuser
However, I wanted to know how I could make it look something like:
www.mysite.com/users/someuser
Thanks in advance!
Have your root .htaccess as this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^users/(\w+)/?$ /users/social.php?username=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
This is only partly a mod_rewrite problem at this point.
The first part is to route URLs like /users/[a-z]+ to some script /social.php
RewriteRule ^/users/(.*)$ social.php?username=$1
The second part is inside social.php you need to get the username "someuser" in $_GET['username'].

URL rewrite through an entry.php

I have two directory under my application directory
my_app:
.htaccess
lib:
entry.php
web:
css:
css_file1
css_file2
js:
js_file1
js_file2
My url will be like this http://example.com/my_app/my_path
Under the directory my_app, there are two directories web and lib.
If the my_path can be map to any file under the web directory, use it.
For example, http://example.com/my_app/css/css_file1 should map to my_app/web/css/css_file1.
Otherwise redirect it to lib/entry.php/my_path so that the the my_path can be accessed through $_SERVER['PATH_INFO'] The redirection should be internal. The redirection should not be visible to user.
I want to write configuration in .htaccess to accomplish it.
I wrote the code as given below.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) ../web/$1
RewriteCond web/%{REQUEST_FILENAME} !-f
RewriteCond web/%{REQUEST_FILENAME} !-d
RewriteRule (.*) lib/entry.php/$1
</IfModule>
But I am getting 403 error.
I believe following should work for you in /var/www/.htaccess i.e. directly under $DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1/web/$2 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1/web/$2 -d
RewriteRule ^([^/]+)/(.*)$ $1/web/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^([^/]+)/(.*)$ $1/lib/entry.php/$2 [L]

Why isn't this mod-rewrite to a different directory working?

I have a link on the main page to
www.example.com/test
I have a page in a pages directory, called test.php
Here's what my .htaccess looks like:
RewriteBase /
RewriteEngine on
RewriteCond pages/%{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.*?)$ pages/$1.php
Now, I can get it to work with RewriteRule ^test$ pages/test.php, but as soon as I put in the regex, it won't work. Also, the RewriteCond rule doesn't seem to work either. Any solutions you can think of? I have tried adding Options +FollowSymlinks -MultiViews but it didn't work.
update
If I add a test.php to the main directory, then change RewriteCond pages/%{REQUEST_FILENAME}.php -f [NC] to RewriteCond %{REQUEST_FILENAME}.php -f [NC] then it works; so that is where the problem is.
I can't figure out how to check if the file exists in the pages directory.
REQUEST_FILENAME is the full server path. So if your document root path is /home/website/public/ you are trying to check if pages/home/website/public/test.php exists, which of course it doesn't.
Here are the rules you want.
RewriteBase /
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/pages/%{REQUEST_URI}.php -f [NC]
RewriteRule ^(.*?)$ pages/$1.php

Tricky .htaccess and Drupal problem

1) I have a Drupal site located at http://example.com and I have a directory located at http://example.com/foo, but I also have a Drupal page with an alias of http://example.com/foo. How can I get the Drupal page to serve? Currently, I get a 403 forbidden page as a result of the Options -Indexes declaration in Drupal's .htaccess file, but I do not want to remove this as I do not want directories to be browsable.
EDIT: I have solved this with the following rule:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
2) To make the problem even more difficult, given the same scenario, if I have an index.html file inside the directory http://example.com/foo/index.html I always want this to take priority over Drupal's aliased page (which it does at the moment - currently I have modified the .htaccess file so that any directory with an index.html file displays it - DirectoryIndex index.php index.html).
EDIT:
So now, how can I write a RewriteCond that will look to see whether or not there is an index.html file inside the directory?
I am no RewriteRule Guru and pretty sure there is a nicer way to achieve this, e.g. with conditional RewriteCond.
But simply adding
RewriteCond %{REQUEST_URI} =/foo/
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Above the default rewrite Conds and rules make this rule kick in for /foo/ and not the Drupal-default one. Where a RewriteCond %{REQUEST_FILENAME} !-d will only apply the RewriteRule if something is NOT a dir.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}index.html -f
RewriteRule ^(.*)$ $1index.html [L]
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*)$ $1/index.html [L]
[normal Drupal rewrite rules]

Categories